xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 41618e63f2a869902f8534f0db337e85d6bd04c8)
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;
5296e47158SLen Brown unsigned int quiet;
53d8af6f5fSLen Brown unsigned int rapl_joules;
54d8af6f5fSLen Brown unsigned int summary_only;
55d8af6f5fSLen Brown unsigned int dump_only;
56103a8feaSLen Brown unsigned int do_snb_cstates;
57fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
580b2bb692SLen Brown unsigned int do_skl_residency;
59144b44b1SLen Brown unsigned int do_slm_cstates;
60144b44b1SLen Brown unsigned int use_c1_residency_msr;
61103a8feaSLen Brown unsigned int has_aperf;
62889facbeSLen Brown unsigned int has_epb;
635a63426eSLen Brown unsigned int do_irtl_snb;
645a63426eSLen Brown unsigned int do_irtl_hsw;
65fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
66103a8feaSLen Brown unsigned int genuine_intel;
67103a8feaSLen Brown unsigned int has_invariant_tsc;
68d7899447SLen Brown unsigned int do_nhm_platform_info;
69cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
70b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
71103a8feaSLen Brown double bclk;
72a2b7b749SLen Brown double base_hz;
7321ed5574SLen Brown unsigned int has_base_hz;
74a2b7b749SLen Brown double tsc_tweak = 1.0;
75c98d5d94SLen Brown unsigned int show_pkg_only;
76c98d5d94SLen Brown unsigned int show_core_only;
77c98d5d94SLen Brown char *output_buffer, *outp;
78889facbeSLen Brown unsigned int do_rapl;
79889facbeSLen Brown unsigned int do_dts;
80889facbeSLen Brown unsigned int do_ptm;
81fdf676e5SLen Brown unsigned long long  gfx_cur_rc6_ms;
8227d47356SLen Brown unsigned int gfx_cur_mhz;
83889facbeSLen Brown unsigned int tcc_activation_temp;
84889facbeSLen Brown unsigned int tcc_activation_temp_override;
8540ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
8640ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
87889facbeSLen Brown double rapl_joule_counter_range;
883a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
893a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
903a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
918a5bdf41SLen Brown unsigned int crystal_hz;
928a5bdf41SLen Brown unsigned long long tsc_hz;
937ce7d5deSPrarit Bhargava int base_cpu;
9421ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
957f5c258eSLen Brown unsigned int has_hwp;	/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
967f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
977f5c258eSLen Brown unsigned int has_hwp_notify;		/* IA32_HWP_INTERRUPT */
987f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
997f5c258eSLen Brown unsigned int has_hwp_epp;		/* IA32_HWP_REQUEST[bits 31:24] */
1007f5c258eSLen Brown unsigned int has_hwp_pkg;		/* IA32_HWP_REQUEST_PKG */
10133148d67SLen Brown unsigned int has_misc_feature_control;
102889facbeSLen Brown 
103889facbeSLen Brown #define RAPL_PKG		(1 << 0)
104e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
105e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
106e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
107e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
108e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
109e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
110e6f9bb3cSLen Brown 
111889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
112e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
113e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
114e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
115e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
1160b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
1170b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
118e6f9bb3cSLen Brown 
1199148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
120e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
1210b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
122e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
123e6f9bb3cSLen Brown 
1240b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
125e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
126e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
127e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
1289148494cSJacob Pan 
1299148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
1309148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
1319148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
132889facbeSLen Brown #define	TJMAX_DEFAULT	100
133889facbeSLen Brown 
134889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
135103a8feaSLen Brown 
136388e9c81SLen Brown /*
137388e9c81SLen Brown  * buffer size used by sscanf() for added column names
138388e9c81SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
139388e9c81SLen Brown  */
140388e9c81SLen Brown #define	NAME_BYTES 20
141495c7654SLen Brown #define PATH_BYTES 128
142388e9c81SLen Brown 
143103a8feaSLen Brown int backwards_count;
144103a8feaSLen Brown char *progname;
145103a8feaSLen Brown 
146c98d5d94SLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set;
147c98d5d94SLen Brown size_t cpu_present_setsize, cpu_affinity_setsize;
148678a3bd1SLen Brown #define MAX_ADDED_COUNTERS 16
149c98d5d94SLen Brown 
150c98d5d94SLen Brown struct thread_data {
151c98d5d94SLen Brown 	unsigned long long tsc;
152c98d5d94SLen Brown 	unsigned long long aperf;
153c98d5d94SLen Brown 	unsigned long long mperf;
154144b44b1SLen Brown 	unsigned long long c1;
155562a2d37SLen Brown 	unsigned int irq_count;
1561ed51011SLen Brown 	unsigned int smi_count;
157c98d5d94SLen Brown 	unsigned int cpu_id;
158c98d5d94SLen Brown 	unsigned int flags;
159c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
160c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
161678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
162c98d5d94SLen Brown } *thread_even, *thread_odd;
163c98d5d94SLen Brown 
164c98d5d94SLen Brown struct core_data {
165c98d5d94SLen Brown 	unsigned long long c3;
166c98d5d94SLen Brown 	unsigned long long c6;
167c98d5d94SLen Brown 	unsigned long long c7;
1680539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
169889facbeSLen Brown 	unsigned int core_temp_c;
170c98d5d94SLen Brown 	unsigned int core_id;
171678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
172c98d5d94SLen Brown } *core_even, *core_odd;
173c98d5d94SLen Brown 
174c98d5d94SLen Brown struct pkg_data {
175c98d5d94SLen Brown 	unsigned long long pc2;
176c98d5d94SLen Brown 	unsigned long long pc3;
177c98d5d94SLen Brown 	unsigned long long pc6;
178c98d5d94SLen Brown 	unsigned long long pc7;
179ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
180ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
181ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
1820b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
1830b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
1840b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
1850b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
1869185e988SLen Brown 	long long gfx_rc6_ms;
18727d47356SLen Brown 	unsigned int gfx_mhz;
188c98d5d94SLen Brown 	unsigned int package_id;
189889facbeSLen Brown 	unsigned int energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
190889facbeSLen Brown 	unsigned int energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
191889facbeSLen Brown 	unsigned int energy_cores;	/* MSR_PP0_ENERGY_STATUS */
192889facbeSLen Brown 	unsigned int energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
193889facbeSLen Brown 	unsigned int rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
194889facbeSLen Brown 	unsigned int rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
195889facbeSLen Brown 	unsigned int pkg_temp_c;
196678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
197c98d5d94SLen Brown } *package_even, *package_odd;
198c98d5d94SLen Brown 
199c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
200c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
201c98d5d94SLen Brown 
202c98d5d94SLen Brown #define GET_THREAD(thread_base, thread_no, core_no, pkg_no) \
203c98d5d94SLen Brown 	(thread_base + (pkg_no) * topo.num_cores_per_pkg * \
204c98d5d94SLen Brown 		topo.num_threads_per_core + \
205c98d5d94SLen Brown 		(core_no) * topo.num_threads_per_core + (thread_no))
206c98d5d94SLen Brown #define GET_CORE(core_base, core_no, pkg_no) \
207c98d5d94SLen Brown 	(core_base + (pkg_no) * topo.num_cores_per_pkg + (core_no))
208c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
209c98d5d94SLen Brown 
210388e9c81SLen Brown enum counter_scope {SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE};
211*41618e63SLen Brown enum counter_type {COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC};
212388e9c81SLen Brown enum counter_format {FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT};
213388e9c81SLen Brown 
214388e9c81SLen Brown struct msr_counter {
215388e9c81SLen Brown 	unsigned int msr_num;
216388e9c81SLen Brown 	char name[NAME_BYTES];
217495c7654SLen Brown 	char path[PATH_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)
225*41618e63SLen Brown #define	SYSFS_PERCPU	(1 << 1)
226388e9c81SLen Brown };
227388e9c81SLen Brown 
228388e9c81SLen Brown struct sys_counters {
229678a3bd1SLen Brown 	unsigned int added_thread_counters;
230678a3bd1SLen Brown 	unsigned int added_core_counters;
231678a3bd1SLen Brown 	unsigned int added_package_counters;
232388e9c81SLen Brown 	struct msr_counter *tp;
233388e9c81SLen Brown 	struct msr_counter *cp;
234388e9c81SLen Brown 	struct msr_counter *pp;
235388e9c81SLen Brown } sys;
236388e9c81SLen Brown 
237c98d5d94SLen Brown struct system_summary {
238c98d5d94SLen Brown 	struct thread_data threads;
239c98d5d94SLen Brown 	struct core_data cores;
240c98d5d94SLen Brown 	struct pkg_data packages;
241388e9c81SLen Brown } average;
242c98d5d94SLen Brown 
243c98d5d94SLen Brown 
244c98d5d94SLen Brown struct topo_params {
245c98d5d94SLen Brown 	int num_packages;
246103a8feaSLen Brown 	int num_cpus;
247c98d5d94SLen Brown 	int num_cores;
248c98d5d94SLen Brown 	int max_cpu_num;
249c98d5d94SLen Brown 	int num_cores_per_pkg;
250c98d5d94SLen Brown 	int num_threads_per_core;
251c98d5d94SLen Brown } topo;
252103a8feaSLen Brown 
253c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
254103a8feaSLen Brown 
255562a2d37SLen Brown int *irq_column_2_cpu;	/* /proc/interrupts column numbers */
256562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
257562a2d37SLen Brown 
258c98d5d94SLen Brown void setup_all_buffers(void);
259103a8feaSLen Brown 
260c98d5d94SLen Brown int cpu_is_not_present(int cpu)
261d15cf7c1SLen Brown {
262c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
263c98d5d94SLen Brown }
264c98d5d94SLen Brown /*
265c98d5d94SLen Brown  * run func(thread, core, package) in topology order
266c98d5d94SLen Brown  * skip non-present cpus
267c98d5d94SLen Brown  */
268c98d5d94SLen Brown 
269c98d5d94SLen Brown int for_all_cpus(int (func)(struct thread_data *, struct core_data *, struct pkg_data *),
270c98d5d94SLen Brown 	struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
271c98d5d94SLen Brown {
272c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
273c98d5d94SLen Brown 
274c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
275c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
276c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
277c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
278c98d5d94SLen Brown 				struct thread_data *t;
279c98d5d94SLen Brown 				struct core_data *c;
280c98d5d94SLen Brown 				struct pkg_data *p;
281c98d5d94SLen Brown 
282c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
283c98d5d94SLen Brown 
284c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
285c98d5d94SLen Brown 					continue;
286c98d5d94SLen Brown 
287c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
288c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
289c98d5d94SLen Brown 
290c98d5d94SLen Brown 				retval = func(t, c, p);
291c98d5d94SLen Brown 				if (retval)
292c98d5d94SLen Brown 					return retval;
293c98d5d94SLen Brown 			}
294c98d5d94SLen Brown 		}
295c98d5d94SLen Brown 	}
296d15cf7c1SLen Brown 	return 0;
297d15cf7c1SLen Brown }
298d15cf7c1SLen Brown 
29988c3281fSLen Brown int cpu_migrate(int cpu)
30088c3281fSLen Brown {
301c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
302c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
303c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
30488c3281fSLen Brown 		return -1;
30588c3281fSLen Brown 	else
30688c3281fSLen Brown 		return 0;
30788c3281fSLen Brown }
30836229897SLen Brown int get_msr_fd(int cpu)
309103a8feaSLen Brown {
310103a8feaSLen Brown 	char pathname[32];
311103a8feaSLen Brown 	int fd;
312103a8feaSLen Brown 
31336229897SLen Brown 	fd = fd_percpu[cpu];
31436229897SLen Brown 
31536229897SLen Brown 	if (fd)
31636229897SLen Brown 		return fd;
31736229897SLen Brown 
318103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
319103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
32015aaa346SLen Brown 	if (fd < 0)
32198481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
322103a8feaSLen Brown 
32336229897SLen Brown 	fd_percpu[cpu] = fd;
32436229897SLen Brown 
32536229897SLen Brown 	return fd;
32636229897SLen Brown }
32736229897SLen Brown 
32836229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
32936229897SLen Brown {
33036229897SLen Brown 	ssize_t retval;
33136229897SLen Brown 
33236229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
33315aaa346SLen Brown 
33498481e79SLen Brown 	if (retval != sizeof *msr)
335cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
33615aaa346SLen Brown 
33715aaa346SLen Brown 	return 0;
338103a8feaSLen Brown }
339103a8feaSLen Brown 
340fc04cc67SLen Brown /*
341812db3f7SLen Brown  * Each string in this array is compared in --show and --hide cmdline.
342812db3f7SLen Brown  * Thus, strings that are proper sub-sets must follow their more specific peers.
343fc04cc67SLen Brown  */
344812db3f7SLen Brown struct msr_counter bic[] = {
345812db3f7SLen Brown 	{ 0x0, "Package" },
346812db3f7SLen Brown 	{ 0x0, "Avg_MHz" },
347812db3f7SLen Brown 	{ 0x0, "Bzy_MHz" },
348812db3f7SLen Brown 	{ 0x0, "TSC_MHz" },
349812db3f7SLen Brown 	{ 0x0, "IRQ" },
350495c7654SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL},
351812db3f7SLen Brown 	{ 0x0, "Busy%" },
352812db3f7SLen Brown 	{ 0x0, "CPU%c1" },
353812db3f7SLen Brown 	{ 0x0, "CPU%c3" },
354812db3f7SLen Brown 	{ 0x0, "CPU%c6" },
355812db3f7SLen Brown 	{ 0x0, "CPU%c7" },
356812db3f7SLen Brown 	{ 0x0, "ThreadC" },
357812db3f7SLen Brown 	{ 0x0, "CoreTmp" },
358812db3f7SLen Brown 	{ 0x0, "CoreCnt" },
359812db3f7SLen Brown 	{ 0x0, "PkgTmp" },
360812db3f7SLen Brown 	{ 0x0, "GFX%rc6" },
361812db3f7SLen Brown 	{ 0x0, "GFXMHz" },
362812db3f7SLen Brown 	{ 0x0, "Pkg%pc2" },
363812db3f7SLen Brown 	{ 0x0, "Pkg%pc3" },
364812db3f7SLen Brown 	{ 0x0, "Pkg%pc6" },
365812db3f7SLen Brown 	{ 0x0, "Pkg%pc7" },
3660f47c08dSLen Brown 	{ 0x0, "Pkg%pc8" },
3670f47c08dSLen Brown 	{ 0x0, "Pkg%pc9" },
3680f47c08dSLen Brown 	{ 0x0, "Pkg%pc10" },
369812db3f7SLen Brown 	{ 0x0, "PkgWatt" },
370812db3f7SLen Brown 	{ 0x0, "CorWatt" },
371812db3f7SLen Brown 	{ 0x0, "GFXWatt" },
372812db3f7SLen Brown 	{ 0x0, "PkgCnt" },
373812db3f7SLen Brown 	{ 0x0, "RAMWatt" },
374812db3f7SLen Brown 	{ 0x0, "PKG_%" },
375812db3f7SLen Brown 	{ 0x0, "RAM_%" },
376812db3f7SLen Brown 	{ 0x0, "Pkg_J" },
377812db3f7SLen Brown 	{ 0x0, "Cor_J" },
378812db3f7SLen Brown 	{ 0x0, "GFX_J" },
379812db3f7SLen Brown 	{ 0x0, "RAM_J" },
380812db3f7SLen Brown 	{ 0x0, "Core" },
381812db3f7SLen Brown 	{ 0x0, "CPU" },
3820539ba11SLen Brown 	{ 0x0, "Mod%c6" },
383*41618e63SLen Brown 	{ 0x0, "sysfs" },
384812db3f7SLen Brown };
385812db3f7SLen Brown 
386812db3f7SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
387812db3f7SLen Brown #define	BIC_Package	(1ULL << 0)
388812db3f7SLen Brown #define	BIC_Avg_MHz	(1ULL << 1)
389812db3f7SLen Brown #define	BIC_Bzy_MHz	(1ULL << 2)
390812db3f7SLen Brown #define	BIC_TSC_MHz	(1ULL << 3)
391812db3f7SLen Brown #define	BIC_IRQ		(1ULL << 4)
392812db3f7SLen Brown #define	BIC_SMI		(1ULL << 5)
393812db3f7SLen Brown #define	BIC_Busy	(1ULL << 6)
394812db3f7SLen Brown #define	BIC_CPU_c1	(1ULL << 7)
395812db3f7SLen Brown #define	BIC_CPU_c3	(1ULL << 8)
396812db3f7SLen Brown #define	BIC_CPU_c6	(1ULL << 9)
397812db3f7SLen Brown #define	BIC_CPU_c7	(1ULL << 10)
398812db3f7SLen Brown #define	BIC_ThreadC	(1ULL << 11)
399812db3f7SLen Brown #define	BIC_CoreTmp	(1ULL << 12)
400812db3f7SLen Brown #define	BIC_CoreCnt	(1ULL << 13)
401812db3f7SLen Brown #define	BIC_PkgTmp	(1ULL << 14)
402812db3f7SLen Brown #define	BIC_GFX_rc6	(1ULL << 15)
403812db3f7SLen Brown #define	BIC_GFXMHz	(1ULL << 16)
404812db3f7SLen Brown #define	BIC_Pkgpc2	(1ULL << 17)
405812db3f7SLen Brown #define	BIC_Pkgpc3	(1ULL << 18)
406812db3f7SLen Brown #define	BIC_Pkgpc6	(1ULL << 19)
407812db3f7SLen Brown #define	BIC_Pkgpc7	(1ULL << 20)
4080f47c08dSLen Brown #define	BIC_Pkgpc8	(1ULL << 21)
4090f47c08dSLen Brown #define	BIC_Pkgpc9	(1ULL << 22)
4100f47c08dSLen Brown #define	BIC_Pkgpc10	(1ULL << 23)
4110f47c08dSLen Brown #define	BIC_PkgWatt	(1ULL << 24)
4120f47c08dSLen Brown #define	BIC_CorWatt	(1ULL << 25)
4130f47c08dSLen Brown #define	BIC_GFXWatt	(1ULL << 26)
4140f47c08dSLen Brown #define	BIC_PkgCnt	(1ULL << 27)
4150f47c08dSLen Brown #define	BIC_RAMWatt	(1ULL << 28)
4160f47c08dSLen Brown #define	BIC_PKG__	(1ULL << 29)
4170f47c08dSLen Brown #define	BIC_RAM__	(1ULL << 30)
4180f47c08dSLen Brown #define	BIC_Pkg_J	(1ULL << 31)
4190f47c08dSLen Brown #define	BIC_Cor_J	(1ULL << 32)
4200f47c08dSLen Brown #define	BIC_GFX_J	(1ULL << 33)
4210f47c08dSLen Brown #define	BIC_RAM_J	(1ULL << 34)
4220f47c08dSLen Brown #define	BIC_Core	(1ULL << 35)
4230f47c08dSLen Brown #define	BIC_CPU		(1ULL << 36)
4240f47c08dSLen Brown #define	BIC_Mod_c6	(1ULL << 37)
425*41618e63SLen Brown #define	BIC_sysfs	(1ULL << 38)
426812db3f7SLen Brown 
427812db3f7SLen Brown unsigned long long bic_enabled = 0xFFFFFFFFFFFFFFFFULL;
428*41618e63SLen Brown unsigned long long bic_present = BIC_sysfs;
429812db3f7SLen Brown 
430812db3f7SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
431812db3f7SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
4320f47c08dSLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
433812db3f7SLen Brown 
434812db3f7SLen Brown /*
435812db3f7SLen Brown  * bic_lookup
436812db3f7SLen Brown  * for all the strings in comma separate name_list,
437812db3f7SLen Brown  * set the approprate bit in return value.
438812db3f7SLen Brown  */
439812db3f7SLen Brown unsigned long long bic_lookup(char *name_list)
440812db3f7SLen Brown {
441812db3f7SLen Brown 	int i;
442812db3f7SLen Brown 	unsigned long long retval = 0;
443812db3f7SLen Brown 
444812db3f7SLen Brown 	while (name_list) {
445812db3f7SLen Brown 		char *comma;
446812db3f7SLen Brown 
447812db3f7SLen Brown 		comma = strchr(name_list, ',');
448812db3f7SLen Brown 
449812db3f7SLen Brown 		if (comma)
450812db3f7SLen Brown 			*comma = '\0';
451812db3f7SLen Brown 
452812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
453812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
454812db3f7SLen Brown 				retval |= (1ULL << i);
455812db3f7SLen Brown 				break;
456812db3f7SLen Brown 			}
457812db3f7SLen Brown 		}
458812db3f7SLen Brown 		if (i == MAX_BIC) {
459812db3f7SLen Brown 			fprintf(stderr, "Invalid counter name: %s\n", name_list);
460812db3f7SLen Brown 			exit(-1);
461812db3f7SLen Brown 		}
462812db3f7SLen Brown 
463812db3f7SLen Brown 		name_list = comma;
464812db3f7SLen Brown 		if (name_list)
465812db3f7SLen Brown 			name_list++;
466812db3f7SLen Brown 
467812db3f7SLen Brown 	}
468812db3f7SLen Brown 	return retval;
469812db3f7SLen Brown }
470fc04cc67SLen Brown 
471a829eb4dSLen Brown void print_header(void)
472103a8feaSLen Brown {
473388e9c81SLen Brown 	struct msr_counter *mp;
474388e9c81SLen Brown 
475812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
4763d109de2SLen Brown 		outp += sprintf(outp, "\tPackage");
477812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
4783d109de2SLen Brown 		outp += sprintf(outp, "\tCore");
479812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
4803d109de2SLen Brown 		outp += sprintf(outp, "\tCPU");
481812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
4823d109de2SLen Brown 		outp += sprintf(outp, "\tAvg_MHz");
483812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
4843d109de2SLen Brown 		outp += sprintf(outp, "\tBusy%%");
485812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
4863d109de2SLen Brown 		outp += sprintf(outp, "\tBzy_MHz");
487812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
4883d109de2SLen Brown 		outp += sprintf(outp, "\tTSC_MHz");
4891cc21f7bSLen Brown 
490812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
4913d109de2SLen Brown 		outp += sprintf(outp, "\tIRQ");
492812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
4933d109de2SLen Brown 		outp += sprintf(outp, "\tSMI");
4941cc21f7bSLen Brown 
495388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
496388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
497388e9c81SLen Brown 			if (mp->width == 64)
498388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
499388e9c81SLen Brown 			else
500388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
501388e9c81SLen Brown 		} else {
502*41618e63SLen Brown 			outp += sprintf(outp, "\t%s", mp->name);
503388e9c81SLen Brown 		}
504388e9c81SLen Brown 	}
505388e9c81SLen Brown 
506*41618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
507*41618e63SLen Brown 		outp += sprintf(outp, "\tCPU%%c1");
508812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
509678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c3");
510812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
511678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c6");
512812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
513678a3bd1SLen Brown 		outp += sprintf(outp, "\tCPU%%c7");
514678a3bd1SLen Brown 
5150539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
5160539ba11SLen Brown 		outp += sprintf(outp, "\tMod%%c6");
517678a3bd1SLen Brown 
518812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
5193d109de2SLen Brown 		outp += sprintf(outp, "\tCoreTmp");
520388e9c81SLen Brown 
521388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
522388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
523388e9c81SLen Brown 			if (mp->width == 64)
524388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
525388e9c81SLen Brown 			else
526388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
527388e9c81SLen Brown 		} else {
528*41618e63SLen Brown 			outp += sprintf(outp, "\t%s", mp->name);
529388e9c81SLen Brown 		}
530388e9c81SLen Brown 	}
531388e9c81SLen Brown 
532812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
5333d109de2SLen Brown 		outp += sprintf(outp, "\tPkgTmp");
534889facbeSLen Brown 
535812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
5363d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%rc6");
537fdf676e5SLen Brown 
538812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
5393d109de2SLen Brown 		outp += sprintf(outp, "\tGFXMHz");
54027d47356SLen Brown 
5410b2bb692SLen Brown 	if (do_skl_residency) {
5423d109de2SLen Brown 		outp += sprintf(outp, "\tTotl%%C0");
5433d109de2SLen Brown 		outp += sprintf(outp, "\tAny%%C0");
5443d109de2SLen Brown 		outp += sprintf(outp, "\tGFX%%C0");
5453d109de2SLen Brown 		outp += sprintf(outp, "\tCPUGFX%%");
5460b2bb692SLen Brown 	}
5470b2bb692SLen Brown 
5480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
5493d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc2");
5500f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
5513d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc3");
5520f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
5533d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc6");
5540f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
5553d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc7");
5560f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
5573d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc8");
5580f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
5593d109de2SLen Brown 		outp += sprintf(outp, "\tPkg%%pc9");
5600f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
5613d109de2SLen Brown 		outp += sprintf(outp, "\tPk%%pc10");
562103a8feaSLen Brown 
5635c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
564812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
5653d109de2SLen Brown 			outp += sprintf(outp, "\tPkgWatt");
566812db3f7SLen Brown 		if (DO_BIC(BIC_CorWatt))
5673d109de2SLen Brown 			outp += sprintf(outp, "\tCorWatt");
568812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
5693d109de2SLen Brown 			outp += sprintf(outp, "\tGFXWatt");
570812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
5713d109de2SLen Brown 			outp += sprintf(outp, "\tRAMWatt");
572812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
5733d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
574812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
5753d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
576d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
577812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
5783d109de2SLen Brown 			outp += sprintf(outp, "\tPkg_J");
579812db3f7SLen Brown 		if (DO_BIC(BIC_Cor_J))
5803d109de2SLen Brown 			outp += sprintf(outp, "\tCor_J");
581812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
5823d109de2SLen Brown 			outp += sprintf(outp, "\tGFX_J");
583812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
5843d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_J");
585812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
5863d109de2SLen Brown 			outp += sprintf(outp, "\tPKG_%%");
587812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
5883d109de2SLen Brown 			outp += sprintf(outp, "\tRAM_%%");
5895c56be9aSDirk Brandewie 	}
590388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
591388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
592388e9c81SLen Brown 			if (mp->width == 64)
593388e9c81SLen Brown 				outp += sprintf(outp, "\t%18.18s", mp->name);
594388e9c81SLen Brown 			else
595388e9c81SLen Brown 				outp += sprintf(outp, "\t%10.10s", mp->name);
596388e9c81SLen Brown 		} else {
597*41618e63SLen Brown 			outp += sprintf(outp, "\t%s", mp->name);
598388e9c81SLen Brown 		}
599388e9c81SLen Brown 	}
600388e9c81SLen Brown 
601c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
602103a8feaSLen Brown }
603103a8feaSLen Brown 
604c98d5d94SLen Brown int dump_counters(struct thread_data *t, struct core_data *c,
605c98d5d94SLen Brown 	struct pkg_data *p)
606103a8feaSLen Brown {
607388e9c81SLen Brown 	int i;
608388e9c81SLen Brown 	struct msr_counter *mp;
609388e9c81SLen Brown 
6103b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
611c98d5d94SLen Brown 
612c98d5d94SLen Brown 	if (t) {
6133b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "CPU: %d flags 0x%x\n",
6143b4d5c7fSAndy Shevchenko 			t->cpu_id, t->flags);
6153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
6163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
6173b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
6183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
6196886fee4SLen Brown 
620812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
621562a2d37SLen Brown 			outp += sprintf(outp, "IRQ: %08X\n", t->irq_count);
622812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
6233b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "SMI: %08X\n", t->smi_count);
624388e9c81SLen Brown 
625388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
626388e9c81SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n",
627388e9c81SLen Brown 				i, mp->msr_num, t->counter[i]);
628388e9c81SLen Brown 		}
629103a8feaSLen Brown 	}
630103a8feaSLen Brown 
631c98d5d94SLen Brown 	if (c) {
6323b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
6333b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
6343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
6353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
6363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
637388e9c81SLen Brown 
638388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
639388e9c81SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n",
640388e9c81SLen Brown 				i, mp->msr_num, c->counter[i]);
641388e9c81SLen Brown 		}
6420539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
643c98d5d94SLen Brown 	}
644103a8feaSLen Brown 
645c98d5d94SLen Brown 	if (p) {
6463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
6470b2bb692SLen Brown 
6480b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
6490b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
6500b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
6510b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
6520b2bb692SLen Brown 
6533b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
6540f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
6553b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
6560f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
6573b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
6580f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
6593b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
6603b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
6613b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
6623b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
6633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules PKG: %0X\n", p->energy_pkg);
6643b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules COR: %0X\n", p->energy_cores);
6653b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules GFX: %0X\n", p->energy_gfx);
6663b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Joules RAM: %0X\n", p->energy_dram);
6673b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle PKG: %0X\n",
6683b4d5c7fSAndy Shevchenko 			p->rapl_pkg_perf_status);
6693b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "Throttle RAM: %0X\n",
6703b4d5c7fSAndy Shevchenko 			p->rapl_dram_perf_status);
6713b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
672388e9c81SLen Brown 
673388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
674388e9c81SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n",
675388e9c81SLen Brown 				i, mp->msr_num, p->counter[i]);
676388e9c81SLen Brown 		}
677c98d5d94SLen Brown 	}
6783b4d5c7fSAndy Shevchenko 
6793b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
6803b4d5c7fSAndy Shevchenko 
681c98d5d94SLen Brown 	return 0;
682103a8feaSLen Brown }
683103a8feaSLen Brown 
684e23da037SLen Brown /*
685e23da037SLen Brown  * column formatting convention & formats
686e23da037SLen Brown  */
687c98d5d94SLen Brown int format_counters(struct thread_data *t, struct core_data *c,
688c98d5d94SLen Brown 	struct pkg_data *p)
689103a8feaSLen Brown {
690008d396eSLen Brown 	double interval_float, tsc;
691fc04cc67SLen Brown 	char *fmt8;
692388e9c81SLen Brown 	int i;
693388e9c81SLen Brown 	struct msr_counter *mp;
694103a8feaSLen Brown 
695c98d5d94SLen Brown 	 /* if showing only 1st thread in core and this isn't one, bail out */
696c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
697c98d5d94SLen Brown 		return 0;
698c98d5d94SLen Brown 
699c98d5d94SLen Brown 	 /* if showing only 1st thread in pkg and this isn't one, bail out */
700c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
701c98d5d94SLen Brown 		return 0;
702c98d5d94SLen Brown 
703103a8feaSLen Brown 	interval_float = tv_delta.tv_sec + tv_delta.tv_usec/1000000.0;
704103a8feaSLen Brown 
705008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
706008d396eSLen Brown 
707c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
708c98d5d94SLen Brown 	if (t == &average.threads) {
709812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
7103d109de2SLen Brown 			outp += sprintf(outp, "\t-");
711812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
7123d109de2SLen Brown 			outp += sprintf(outp, "\t-");
713812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
7143d109de2SLen Brown 			outp += sprintf(outp, "\t-");
715103a8feaSLen Brown 	} else {
716812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
717c98d5d94SLen Brown 			if (p)
7183d109de2SLen Brown 				outp += sprintf(outp, "\t%d", p->package_id);
719c98d5d94SLen Brown 			else
7203d109de2SLen Brown 				outp += sprintf(outp, "\t-");
721c98d5d94SLen Brown 		}
722812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
723c98d5d94SLen Brown 			if (c)
7243d109de2SLen Brown 				outp += sprintf(outp, "\t%d", c->core_id);
725c98d5d94SLen Brown 			else
7263d109de2SLen Brown 				outp += sprintf(outp, "\t-");
727c98d5d94SLen Brown 		}
728812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
7293d109de2SLen Brown 			outp += sprintf(outp, "\t%d", t->cpu_id);
730103a8feaSLen Brown 	}
731fc04cc67SLen Brown 
732812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
7333d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f",
734fc04cc67SLen Brown 			1.0 / units * t->aperf / interval_float);
735fc04cc67SLen Brown 
736812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
737008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->mperf/tsc);
738103a8feaSLen Brown 
739812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
74021ed5574SLen Brown 		if (has_base_hz)
7413d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f", base_hz / units * t->aperf / t->mperf);
74221ed5574SLen Brown 		else
7433d109de2SLen Brown 			outp += sprintf(outp, "\t%.0f",
744008d396eSLen Brown 				tsc / units * t->aperf / t->mperf / interval_float);
74521ed5574SLen Brown 	}
746103a8feaSLen Brown 
747812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
7483d109de2SLen Brown 		outp += sprintf(outp, "\t%.0f", 1.0 * t->tsc/units/interval_float);
749103a8feaSLen Brown 
750562a2d37SLen Brown 	/* IRQ */
751812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
7523d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->irq_count);
753562a2d37SLen Brown 
7541cc21f7bSLen Brown 	/* SMI */
755812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
7563d109de2SLen Brown 		outp += sprintf(outp, "\t%d", t->smi_count);
7571cc21f7bSLen Brown 
758678a3bd1SLen Brown 	/* Added counters */
759678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
760678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
761678a3bd1SLen Brown 			if (mp->width == 32)
762678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) t->counter[i]);
763678a3bd1SLen Brown 			else
764678a3bd1SLen Brown 				outp += sprintf(outp, "\t0x%016llx", t->counter[i]);
765678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
766678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", t->counter[i]);
767678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
768*41618e63SLen Brown 			if (mp->type == COUNTER_USEC)
769*41618e63SLen Brown 				outp += sprintf(outp, "\t%.2f", t->counter[i]/interval_float/10000);
770*41618e63SLen Brown 			else
771008d396eSLen Brown 				outp += sprintf(outp, "\t%.2f", 100.0 * t->counter[i]/tsc);
772678a3bd1SLen Brown 		}
773678a3bd1SLen Brown 	}
774678a3bd1SLen Brown 
775*41618e63SLen Brown 	/* C1 */
776*41618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
777*41618e63SLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * t->c1/tsc);
778*41618e63SLen Brown 
779*41618e63SLen Brown 
780c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
781c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
782c98d5d94SLen Brown 		goto done;
783c98d5d94SLen Brown 
784812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates)
785008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c3/tsc);
786812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
787008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c6/tsc);
788812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
789008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->c7/tsc);
790c98d5d94SLen Brown 
7910539ba11SLen Brown 	/* Mod%c6 */
7920539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
793008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * c->mc6_us / tsc);
7940539ba11SLen Brown 
795812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
7963d109de2SLen Brown 		outp += sprintf(outp, "\t%d", c->core_temp_c);
797889facbeSLen Brown 
798388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
799388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
800388e9c81SLen Brown 			if (mp->width == 32)
801388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) c->counter[i]);
802388e9c81SLen Brown 			else
803388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", c->counter[i]);
804388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
805678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", c->counter[i]);
806388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
807008d396eSLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * c->counter[i]/tsc);
808388e9c81SLen Brown 		}
809388e9c81SLen Brown 	}
810388e9c81SLen Brown 
811c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
812c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
813c98d5d94SLen Brown 		goto done;
814c98d5d94SLen Brown 
8150b2bb692SLen Brown 	/* PkgTmp */
816812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
8173d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->pkg_temp_c);
818889facbeSLen Brown 
819fdf676e5SLen Brown 	/* GFXrc6 */
820812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
821ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
8223d109de2SLen Brown 			outp += sprintf(outp, "\t**.**");
8239185e988SLen Brown 		} else {
8243d109de2SLen Brown 			outp += sprintf(outp, "\t%.2f",
8259185e988SLen Brown 				p->gfx_rc6_ms / 10.0 / interval_float);
8269185e988SLen Brown 		}
8279185e988SLen Brown 	}
828fdf676e5SLen Brown 
82927d47356SLen Brown 	/* GFXMHz */
830812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
8313d109de2SLen Brown 		outp += sprintf(outp, "\t%d", p->gfx_mhz);
83227d47356SLen Brown 
8330b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
8340b2bb692SLen Brown 	if (do_skl_residency) {
835008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_wtd_core_c0/tsc);
836008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_core_c0/tsc);
837008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_any_gfxe_c0/tsc);
838008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pkg_both_core_gfxe_c0/tsc);
8390b2bb692SLen Brown 	}
8400b2bb692SLen Brown 
8410f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
842008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc2/tsc);
8430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
844008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc3/tsc);
8450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
846008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc6/tsc);
8470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
848008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc7/tsc);
8490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
850008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc8/tsc);
8510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
852008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc9/tsc);
8530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
854008d396eSLen Brown 		outp += sprintf(outp, "\t%.2f", 100.0 * p->pc10/tsc);
855889facbeSLen Brown 
856889facbeSLen Brown 	/*
857889facbeSLen Brown  	 * If measurement interval exceeds minimum RAPL Joule Counter range,
858889facbeSLen Brown  	 * indicate that results are suspect by printing "**" in fraction place.
859889facbeSLen Brown  	 */
860fc04cc67SLen Brown 	if (interval_float < rapl_joule_counter_range)
8613d109de2SLen Brown 		fmt8 = "\t%.2f";
862fc04cc67SLen Brown 	else
863fc04cc67SLen Brown 		fmt8 = "%6.0f**";
864889facbeSLen Brown 
865812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
866fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units / interval_float);
867812db3f7SLen Brown 	if (DO_BIC(BIC_CorWatt))
868fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units / interval_float);
869812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
870fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units / interval_float);
871812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
87240ee8e3bSAndrey Semin 		outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units / interval_float);
873812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
874812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_pkg * rapl_energy_units);
875812db3f7SLen Brown 	if (DO_BIC(BIC_Cor_J))
876812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_cores * rapl_energy_units);
877812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
878812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_gfx * rapl_energy_units);
879812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
880812db3f7SLen Brown 		outp += sprintf(outp, fmt8, p->energy_dram * rapl_dram_energy_units);
881812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
882fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, 100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
883812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
884fc04cc67SLen Brown 		outp += sprintf(outp, fmt8, 100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
885812db3f7SLen Brown 
886388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
887388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
888388e9c81SLen Brown 			if (mp->width == 32)
889388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%08lx", (unsigned long) p->counter[i]);
890388e9c81SLen Brown 			else
891388e9c81SLen Brown 				outp += sprintf(outp, "\t0x%016llx", p->counter[i]);
892388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
893678a3bd1SLen Brown 			outp += sprintf(outp, "\t%lld", p->counter[i]);
894388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
895008d396eSLen Brown 			outp += sprintf(outp, "\t%.2f", 100.0 * p->counter[i]/tsc);
896388e9c81SLen Brown 		}
897388e9c81SLen Brown 	}
898388e9c81SLen Brown 
899c98d5d94SLen Brown done:
900c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
901c98d5d94SLen Brown 
902c98d5d94SLen Brown 	return 0;
903103a8feaSLen Brown }
904103a8feaSLen Brown 
905b7d8c148SLen Brown void flush_output_stdout(void)
906103a8feaSLen Brown {
907b7d8c148SLen Brown 	FILE *filep;
908b7d8c148SLen Brown 
909b7d8c148SLen Brown 	if (outf == stderr)
910b7d8c148SLen Brown 		filep = stdout;
911b7d8c148SLen Brown 	else
912b7d8c148SLen Brown 		filep = outf;
913b7d8c148SLen Brown 
914b7d8c148SLen Brown 	fputs(output_buffer, filep);
915b7d8c148SLen Brown 	fflush(filep);
916b7d8c148SLen Brown 
917c98d5d94SLen Brown 	outp = output_buffer;
918c98d5d94SLen Brown }
919b7d8c148SLen Brown void flush_output_stderr(void)
920c98d5d94SLen Brown {
921b7d8c148SLen Brown 	fputs(output_buffer, outf);
922b7d8c148SLen Brown 	fflush(outf);
923c98d5d94SLen Brown 	outp = output_buffer;
924c98d5d94SLen Brown }
925c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
926c98d5d94SLen Brown {
927e23da037SLen Brown 	static int printed;
928103a8feaSLen Brown 
929e23da037SLen Brown 	if (!printed || !summary_only)
930103a8feaSLen Brown 		print_header();
931103a8feaSLen Brown 
932c98d5d94SLen Brown 	if (topo.num_cpus > 1)
933c98d5d94SLen Brown 		format_counters(&average.threads, &average.cores,
934c98d5d94SLen Brown 			&average.packages);
935103a8feaSLen Brown 
936e23da037SLen Brown 	printed = 1;
937e23da037SLen Brown 
938e23da037SLen Brown 	if (summary_only)
939e23da037SLen Brown 		return;
940e23da037SLen Brown 
941c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
942103a8feaSLen Brown }
943103a8feaSLen Brown 
944889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
945889facbeSLen Brown 	if (new > old) {			\
946889facbeSLen Brown 		old = new - old;		\
947889facbeSLen Brown 	} else {				\
948889facbeSLen Brown 		old = 0x100000000 + new - old;	\
949889facbeSLen Brown 	}
950889facbeSLen Brown 
951ba3dec99SLen Brown int
952c98d5d94SLen Brown delta_package(struct pkg_data *new, struct pkg_data *old)
953103a8feaSLen Brown {
954388e9c81SLen Brown 	int i;
955388e9c81SLen Brown 	struct msr_counter *mp;
9560b2bb692SLen Brown 
9570b2bb692SLen Brown 	if (do_skl_residency) {
9580b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
9590b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
9600b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
9610b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
9620b2bb692SLen Brown 	}
963c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
9640f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
965c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
9660f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
967c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
9680f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
969c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
970ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
971ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
972ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
973889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
974889facbeSLen Brown 
9759185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
9769185e988SLen Brown 	if (old->gfx_rc6_ms >  new->gfx_rc6_ms)
9779185e988SLen Brown 		old->gfx_rc6_ms = -1;
9789185e988SLen Brown 	else
979fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
9809185e988SLen Brown 
98127d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
98227d47356SLen Brown 
983889facbeSLen Brown 	DELTA_WRAP32(new->energy_pkg, old->energy_pkg);
984889facbeSLen Brown 	DELTA_WRAP32(new->energy_cores, old->energy_cores);
985889facbeSLen Brown 	DELTA_WRAP32(new->energy_gfx, old->energy_gfx);
986889facbeSLen Brown 	DELTA_WRAP32(new->energy_dram, old->energy_dram);
987889facbeSLen Brown 	DELTA_WRAP32(new->rapl_pkg_perf_status, old->rapl_pkg_perf_status);
988889facbeSLen Brown 	DELTA_WRAP32(new->rapl_dram_perf_status, old->rapl_dram_perf_status);
989ba3dec99SLen Brown 
990388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
991388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
992388e9c81SLen Brown 			old->counter[i] = new->counter[i];
993388e9c81SLen Brown 		else
994388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
995388e9c81SLen Brown 	}
996388e9c81SLen Brown 
997ba3dec99SLen Brown 	return 0;
998103a8feaSLen Brown }
999103a8feaSLen Brown 
1000c98d5d94SLen Brown void
1001c98d5d94SLen Brown delta_core(struct core_data *new, struct core_data *old)
1002c98d5d94SLen Brown {
1003388e9c81SLen Brown 	int i;
1004388e9c81SLen Brown 	struct msr_counter *mp;
1005388e9c81SLen Brown 
1006c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1007c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1008c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1009889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
10100539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1011388e9c81SLen Brown 
1012388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1013388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1014388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1015388e9c81SLen Brown 		else
1016388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1017388e9c81SLen Brown 	}
1018103a8feaSLen Brown }
1019c98d5d94SLen Brown 
1020c3ae331dSLen Brown /*
1021c3ae331dSLen Brown  * old = new - old
1022c3ae331dSLen Brown  */
1023ba3dec99SLen Brown int
1024c98d5d94SLen Brown delta_thread(struct thread_data *new, struct thread_data *old,
1025c98d5d94SLen Brown 	struct core_data *core_delta)
1026c98d5d94SLen Brown {
1027388e9c81SLen Brown 	int i;
1028388e9c81SLen Brown 	struct msr_counter *mp;
1029388e9c81SLen Brown 
1030c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1031c98d5d94SLen Brown 
1032103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1033b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1034b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1035b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1036b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1037103a8feaSLen Brown 
1038c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1039c98d5d94SLen Brown 
1040812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
1041c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1042c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1043c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1044c98d5d94SLen Brown 		} else {
1045ba3dec99SLen Brown 			return -1;
1046103a8feaSLen Brown 		}
1047a729617cSLen Brown 	}
1048103a8feaSLen Brown 
1049c98d5d94SLen Brown 
1050144b44b1SLen Brown 	if (use_c1_residency_msr) {
1051144b44b1SLen Brown 		/*
1052144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1053144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1054144b44b1SLen Brown 		 */
1055144b44b1SLen Brown 	} else {
1056103a8feaSLen Brown 		/*
1057c3ae331dSLen Brown 		 * As counter collection is not atomic,
1058c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1059103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1060103a8feaSLen Brown 		 */
1061c3ae331dSLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > old->tsc)
1062c98d5d94SLen Brown 			old->c1 = 0;
1063c98d5d94SLen Brown 		else {
1064c98d5d94SLen Brown 			/* normal case, derive c1 */
1065008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1066c98d5d94SLen Brown 				- core_delta->c6 - core_delta->c7;
1067c98d5d94SLen Brown 		}
1068144b44b1SLen Brown 	}
1069c3ae331dSLen Brown 
1070c98d5d94SLen Brown 	if (old->mperf == 0) {
1071b7d8c148SLen Brown 		if (debug > 1)
1072b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1073c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1074c98d5d94SLen Brown 	}
1075103a8feaSLen Brown 
1076812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1077562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1078562a2d37SLen Brown 
1079812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
10801ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1081ba3dec99SLen Brown 
1082388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1083388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1084388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1085388e9c81SLen Brown 		else
1086388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1087388e9c81SLen Brown 	}
1088ba3dec99SLen Brown 	return 0;
1089103a8feaSLen Brown }
1090c98d5d94SLen Brown 
1091c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
1092c98d5d94SLen Brown 	struct pkg_data *p, struct thread_data *t2,
1093c98d5d94SLen Brown 	struct core_data *c2, struct pkg_data *p2)
1094c98d5d94SLen Brown {
1095ba3dec99SLen Brown 	int retval = 0;
1096ba3dec99SLen Brown 
1097c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1098c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1099c98d5d94SLen Brown 		delta_core(c, c2);
1100c98d5d94SLen Brown 
1101c98d5d94SLen Brown 	/* always calculate thread delta */
1102ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1103ba3dec99SLen Brown 	if (retval)
1104ba3dec99SLen Brown 		return retval;
1105c98d5d94SLen Brown 
1106c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1107c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1108ba3dec99SLen Brown 		retval = delta_package(p, p2);
1109c98d5d94SLen Brown 
1110ba3dec99SLen Brown 	return retval;
1111103a8feaSLen Brown }
1112103a8feaSLen Brown 
1113c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1114103a8feaSLen Brown {
1115388e9c81SLen Brown 	int i;
1116388e9c81SLen Brown 	struct msr_counter  *mp;
1117388e9c81SLen Brown 
1118c98d5d94SLen Brown 	t->tsc = 0;
1119c98d5d94SLen Brown 	t->aperf = 0;
1120c98d5d94SLen Brown 	t->mperf = 0;
1121c98d5d94SLen Brown 	t->c1 = 0;
1122103a8feaSLen Brown 
1123562a2d37SLen Brown 	t->irq_count = 0;
1124562a2d37SLen Brown 	t->smi_count = 0;
1125562a2d37SLen Brown 
1126c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1127c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1128c98d5d94SLen Brown 
1129c98d5d94SLen Brown 	c->c3 = 0;
1130c98d5d94SLen Brown 	c->c6 = 0;
1131c98d5d94SLen Brown 	c->c7 = 0;
11320539ba11SLen Brown 	c->mc6_us = 0;
1133889facbeSLen Brown 	c->core_temp_c = 0;
1134c98d5d94SLen Brown 
11350b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
11360b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
11370b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
11380b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
11390b2bb692SLen Brown 
1140c98d5d94SLen Brown 	p->pc2 = 0;
11410f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1142c98d5d94SLen Brown 		p->pc3 = 0;
11430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1144c98d5d94SLen Brown 		p->pc6 = 0;
11450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1146c98d5d94SLen Brown 		p->pc7 = 0;
1147ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1148ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1149ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1150889facbeSLen Brown 
1151889facbeSLen Brown 	p->energy_pkg = 0;
1152889facbeSLen Brown 	p->energy_dram = 0;
1153889facbeSLen Brown 	p->energy_cores = 0;
1154889facbeSLen Brown 	p->energy_gfx = 0;
1155889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1156889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1157889facbeSLen Brown 	p->pkg_temp_c = 0;
115827d47356SLen Brown 
1159fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
116027d47356SLen Brown 	p->gfx_mhz = 0;
1161388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1162388e9c81SLen Brown 		t->counter[i] = 0;
1163388e9c81SLen Brown 
1164388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1165388e9c81SLen Brown 		c->counter[i] = 0;
1166388e9c81SLen Brown 
1167388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1168388e9c81SLen Brown 		p->counter[i] = 0;
1169103a8feaSLen Brown }
1170c98d5d94SLen Brown int sum_counters(struct thread_data *t, struct core_data *c,
1171c98d5d94SLen Brown 	struct pkg_data *p)
1172103a8feaSLen Brown {
1173388e9c81SLen Brown 	int i;
1174388e9c81SLen Brown 	struct msr_counter *mp;
1175388e9c81SLen Brown 
1176c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1177c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1178c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1179c98d5d94SLen Brown 	average.threads.c1 += t->c1;
118015aaa346SLen Brown 
1181562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1182562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1183562a2d37SLen Brown 
1184388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1185388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1186388e9c81SLen Brown 			continue;
1187388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1188388e9c81SLen Brown 	}
1189388e9c81SLen Brown 
1190c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1191c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1192c98d5d94SLen Brown 		return 0;
1193c98d5d94SLen Brown 
1194c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1195c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1196c98d5d94SLen Brown 	average.cores.c7 += c->c7;
11970539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1198c98d5d94SLen Brown 
1199889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1200889facbeSLen Brown 
1201388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1202388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1203388e9c81SLen Brown 			continue;
1204388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1205388e9c81SLen Brown 	}
1206388e9c81SLen Brown 
1207c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1208c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1209c98d5d94SLen Brown 		return 0;
1210c98d5d94SLen Brown 
12110b2bb692SLen Brown 	if (do_skl_residency) {
12120b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
12130b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
12140b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
12150b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
12160b2bb692SLen Brown 	}
12170b2bb692SLen Brown 
1218c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
12190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1220c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
12210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1222c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
12230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1224c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1225ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1226ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1227ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1228c98d5d94SLen Brown 
1229889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1230889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1231889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1232889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1233889facbeSLen Brown 
1234fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
123527d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
123627d47356SLen Brown 
1237889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1238889facbeSLen Brown 
1239889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1240889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1241388e9c81SLen Brown 
1242388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1243388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1244388e9c81SLen Brown 			continue;
1245388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1246388e9c81SLen Brown 	}
1247c98d5d94SLen Brown 	return 0;
1248c98d5d94SLen Brown }
1249c98d5d94SLen Brown /*
1250c98d5d94SLen Brown  * sum the counters for all cpus in the system
1251c98d5d94SLen Brown  * compute the weighted average
1252c98d5d94SLen Brown  */
1253c98d5d94SLen Brown void compute_average(struct thread_data *t, struct core_data *c,
1254c98d5d94SLen Brown 	struct pkg_data *p)
1255c98d5d94SLen Brown {
1256388e9c81SLen Brown 	int i;
1257388e9c81SLen Brown 	struct msr_counter *mp;
1258388e9c81SLen Brown 
1259c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1260c98d5d94SLen Brown 
1261c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1262c98d5d94SLen Brown 
1263c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1264c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1265c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
1266c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1267c98d5d94SLen Brown 
1268c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1269c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1270c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
12710539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1272c98d5d94SLen Brown 
12730b2bb692SLen Brown 	if (do_skl_residency) {
12740b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
12750b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
12760b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
12770b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
12780b2bb692SLen Brown 	}
12790b2bb692SLen Brown 
1280c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
12810f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1282c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
12830f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1284c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
12850f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1286c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1287ca58710fSKristen Carlson Accardi 
1288ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1289ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1290ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1291388e9c81SLen Brown 
1292388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1293388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1294388e9c81SLen Brown 			continue;
1295*41618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU && mp->type == COUNTER_ITEMS)
1296*41618e63SLen Brown 			continue;
1297388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1298388e9c81SLen Brown 	}
1299388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1300388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1301388e9c81SLen Brown 			continue;
1302388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1303388e9c81SLen Brown 	}
1304388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1305388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1306388e9c81SLen Brown 			continue;
1307388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1308388e9c81SLen Brown 	}
1309c98d5d94SLen Brown }
1310c98d5d94SLen Brown 
1311c98d5d94SLen Brown static unsigned long long rdtsc(void)
1312c98d5d94SLen Brown {
1313c98d5d94SLen Brown 	unsigned int low, high;
1314c98d5d94SLen Brown 
1315c98d5d94SLen Brown 	asm volatile("rdtsc" : "=a" (low), "=d" (high));
1316c98d5d94SLen Brown 
1317c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1318c98d5d94SLen Brown }
1319c98d5d94SLen Brown 
1320c98d5d94SLen Brown /*
1321495c7654SLen Brown  * Open a file, and exit on failure
1322495c7654SLen Brown  */
1323495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1324495c7654SLen Brown {
1325495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1326495c7654SLen Brown 
1327495c7654SLen Brown 	if (!filep)
1328495c7654SLen Brown 		err(1, "%s: open failed", path);
1329495c7654SLen Brown 	return filep;
1330495c7654SLen Brown }
1331495c7654SLen Brown /*
1332495c7654SLen Brown  * snapshot_sysfs_counter()
1333495c7654SLen Brown  *
1334495c7654SLen Brown  * return snapshot of given counter
1335495c7654SLen Brown  */
1336495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1337495c7654SLen Brown {
1338495c7654SLen Brown 	FILE *fp;
1339495c7654SLen Brown 	int retval;
1340495c7654SLen Brown 	unsigned long long counter;
1341495c7654SLen Brown 
1342495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1343495c7654SLen Brown 
1344495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1345495c7654SLen Brown 	if (retval != 1)
1346495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1347495c7654SLen Brown 
1348495c7654SLen Brown 	fclose(fp);
1349495c7654SLen Brown 
1350495c7654SLen Brown 	return counter;
1351495c7654SLen Brown }
1352495c7654SLen Brown 
1353495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1354495c7654SLen Brown {
1355495c7654SLen Brown 	if (mp->msr_num != 0) {
1356495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1357495c7654SLen Brown 			return -1;
1358495c7654SLen Brown 	} else {
1359*41618e63SLen Brown 		char path[128];
1360*41618e63SLen Brown 
1361*41618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
1362*41618e63SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s",
1363*41618e63SLen Brown 				 cpu, mp->path);
1364*41618e63SLen Brown 
1365*41618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
1366*41618e63SLen Brown 		} else {
1367495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1368495c7654SLen Brown 		}
1369*41618e63SLen Brown 	}
1370495c7654SLen Brown 
1371495c7654SLen Brown 	return 0;
1372495c7654SLen Brown }
1373495c7654SLen Brown 
1374495c7654SLen Brown /*
1375c98d5d94SLen Brown  * get_counters(...)
1376c98d5d94SLen Brown  * migrate to cpu
1377c98d5d94SLen Brown  * acquire and record local counters for that cpu
1378c98d5d94SLen Brown  */
1379c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1380c98d5d94SLen Brown {
1381c98d5d94SLen Brown 	int cpu = t->cpu_id;
1382889facbeSLen Brown 	unsigned long long msr;
13830102b067SLen Brown 	int aperf_mperf_retry_count = 0;
1384388e9c81SLen Brown 	struct msr_counter *mp;
1385388e9c81SLen Brown 	int i;
1386c98d5d94SLen Brown 
1387e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
1388b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
138915aaa346SLen Brown 		return -1;
1390e52966c0SLen Brown 	}
139115aaa346SLen Brown 
13920102b067SLen Brown retry:
1393c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
139415aaa346SLen Brown 
1395812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz)) {
13960102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
13970102b067SLen Brown 
13980102b067SLen Brown 		/*
13990102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
14000102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
14010102b067SLen Brown 		 *
14020102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
14030102b067SLen Brown 		 * individual system call, so delays may occur
14040102b067SLen Brown 		 * between them.  If the time to read them
14050102b067SLen Brown 		 * varies by a large amount, we re-read them.
14060102b067SLen Brown 		 */
14070102b067SLen Brown 
14080102b067SLen Brown 		/*
14090102b067SLen Brown 		 * This initial dummy APERF read has been seen to
14100102b067SLen Brown 		 * reduce jitter in the subsequent reads.
14110102b067SLen Brown 		 */
14120102b067SLen Brown 
14139c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
1414c98d5d94SLen Brown 			return -3;
14150102b067SLen Brown 
14160102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
14170102b067SLen Brown 
14180102b067SLen Brown 		tsc_before = t->tsc;
14190102b067SLen Brown 
14200102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
14210102b067SLen Brown 			return -3;
14220102b067SLen Brown 
14230102b067SLen Brown 		tsc_between = rdtsc();
14240102b067SLen Brown 
14259c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
1426c98d5d94SLen Brown 			return -4;
14270102b067SLen Brown 
14280102b067SLen Brown 		tsc_after = rdtsc();
14290102b067SLen Brown 
14300102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
14310102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
14320102b067SLen Brown 
14330102b067SLen Brown 		/*
14340102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
14350102b067SLen Brown 		 * differ by more than 2x, then try again.
14360102b067SLen Brown 		 */
14370102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
14380102b067SLen Brown 			aperf_mperf_retry_count++;
14390102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
14400102b067SLen Brown 				goto retry;
14410102b067SLen Brown 			else
14420102b067SLen Brown 				warnx("cpu%d jitter %lld %lld",
14430102b067SLen Brown 					cpu, aperf_time, mperf_time);
14440102b067SLen Brown 		}
14450102b067SLen Brown 		aperf_mperf_retry_count = 0;
14460102b067SLen Brown 
1447b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
1448b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
144988c3281fSLen Brown 	}
145088c3281fSLen Brown 
1451812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1452562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
1453812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
14541ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
14551ed51011SLen Brown 			return -5;
14561ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
14571ed51011SLen Brown 	}
14580539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
1459144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
1460144b44b1SLen Brown 			return -6;
1461144b44b1SLen Brown 	}
1462144b44b1SLen Brown 
1463388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1464495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
1465388e9c81SLen Brown 			return -10;
1466388e9c81SLen Brown 	}
1467388e9c81SLen Brown 
1468c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
1469c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1470c98d5d94SLen Brown 		return 0;
1471c98d5d94SLen Brown 
1472812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c3) && !do_slm_cstates && !do_knl_cstates) {
1473c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
1474c98d5d94SLen Brown 			return -6;
1475144b44b1SLen Brown 	}
1476144b44b1SLen Brown 
1477812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6) && !do_knl_cstates) {
1478c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
1479c98d5d94SLen Brown 			return -7;
1480fb5d4327SDasaratharaman Chandramouli 	} else if (do_knl_cstates) {
1481fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
1482fb5d4327SDasaratharaman Chandramouli 			return -7;
1483103a8feaSLen Brown 	}
148415aaa346SLen Brown 
1485812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
1486c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
1487c98d5d94SLen Brown 			return -8;
1488c98d5d94SLen Brown 
14890539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
14900539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
14910539ba11SLen Brown 			return -8;
14920539ba11SLen Brown 
1493812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
1494889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
1495889facbeSLen Brown 			return -9;
1496889facbeSLen Brown 		c->core_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1497889facbeSLen Brown 	}
1498889facbeSLen Brown 
1499388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1500495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
1501388e9c81SLen Brown 			return -10;
1502388e9c81SLen Brown 	}
1503889facbeSLen Brown 
1504c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
1505c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1506c98d5d94SLen Brown 		return 0;
150715aaa346SLen Brown 
15080b2bb692SLen Brown 	if (do_skl_residency) {
15090b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
15100b2bb692SLen Brown 			return -10;
15110b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
15120b2bb692SLen Brown 			return -11;
15130b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
15140b2bb692SLen Brown 			return -12;
15150b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
15160b2bb692SLen Brown 			return -13;
15170b2bb692SLen Brown 	}
15180f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1519c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
1520c98d5d94SLen Brown 			return -9;
15210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
15220539ba11SLen Brown 		if (do_slm_cstates) {
15230539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
15240539ba11SLen Brown 				return -10;
15250539ba11SLen Brown 		} else {
1526c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
1527c98d5d94SLen Brown 				return -10;
15280539ba11SLen Brown 		}
15290539ba11SLen Brown 	}
15300539ba11SLen Brown 
15310f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
1532c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
1533c98d5d94SLen Brown 			return -11;
15340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1535c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
1536c98d5d94SLen Brown 			return -12;
15370f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
1538ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
1539ca58710fSKristen Carlson Accardi 			return -13;
15400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
1541ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
1542ca58710fSKristen Carlson Accardi 			return -13;
15430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
1544ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
1545ca58710fSKristen Carlson Accardi 			return -13;
15460f47c08dSLen Brown 
1547889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
1548889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_ENERGY_STATUS, &msr))
1549889facbeSLen Brown 			return -13;
1550889facbeSLen Brown 		p->energy_pkg = msr & 0xFFFFFFFF;
1551889facbeSLen Brown 	}
15529148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
1553889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_ENERGY_STATUS, &msr))
1554889facbeSLen Brown 			return -14;
1555889facbeSLen Brown 		p->energy_cores = msr & 0xFFFFFFFF;
1556889facbeSLen Brown 	}
1557889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
1558889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
1559889facbeSLen Brown 			return -15;
1560889facbeSLen Brown 		p->energy_dram = msr & 0xFFFFFFFF;
1561889facbeSLen Brown 	}
1562889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
1563889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_ENERGY_STATUS, &msr))
1564889facbeSLen Brown 			return -16;
1565889facbeSLen Brown 		p->energy_gfx = msr & 0xFFFFFFFF;
1566889facbeSLen Brown 	}
1567889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
1568889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_PERF_STATUS, &msr))
1569889facbeSLen Brown 			return -16;
1570889facbeSLen Brown 		p->rapl_pkg_perf_status = msr & 0xFFFFFFFF;
1571889facbeSLen Brown 	}
1572889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
1573889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_PERF_STATUS, &msr))
1574889facbeSLen Brown 			return -16;
1575889facbeSLen Brown 		p->rapl_dram_perf_status = msr & 0xFFFFFFFF;
1576889facbeSLen Brown 	}
1577812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
1578889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
1579889facbeSLen Brown 			return -17;
1580889facbeSLen Brown 		p->pkg_temp_c = tcc_activation_temp - ((msr >> 16) & 0x7F);
1581889facbeSLen Brown 	}
1582fdf676e5SLen Brown 
1583812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
1584fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
1585fdf676e5SLen Brown 
1586812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
158727d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
158827d47356SLen Brown 
1589388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1590495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
1591388e9c81SLen Brown 			return -10;
1592388e9c81SLen Brown 	}
1593388e9c81SLen Brown 
159415aaa346SLen Brown 	return 0;
1595103a8feaSLen Brown }
1596103a8feaSLen Brown 
1597ee7e38e3SLen Brown /*
1598ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
1599ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
1600ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
1601ee7e38e3SLen Brown  */
1602ee7e38e3SLen Brown 
1603ee7e38e3SLen Brown #define PCLUKN 0 /* Unknown */
1604ee7e38e3SLen Brown #define PCLRSV 1 /* Reserved */
1605ee7e38e3SLen Brown #define PCL__0 2 /* PC0 */
1606ee7e38e3SLen Brown #define PCL__1 3 /* PC1 */
1607ee7e38e3SLen Brown #define PCL__2 4 /* PC2 */
1608ee7e38e3SLen Brown #define PCL__3 5 /* PC3 */
1609ee7e38e3SLen Brown #define PCL__4 6 /* PC4 */
1610ee7e38e3SLen Brown #define PCL__6 7 /* PC6 */
1611ee7e38e3SLen Brown #define PCL_6N 8 /* PC6 No Retention */
1612ee7e38e3SLen Brown #define PCL_6R 9 /* PC6 Retention */
1613ee7e38e3SLen Brown #define PCL__7 10 /* PC7 */
1614ee7e38e3SLen Brown #define PCL_7S 11 /* PC7 Shrink */
16150b2bb692SLen Brown #define PCL__8 12 /* PC8 */
16160b2bb692SLen Brown #define PCL__9 13 /* PC9 */
16170b2bb692SLen Brown #define PCLUNL 14 /* Unlimited */
1618ee7e38e3SLen Brown 
1619ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
1620ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
16210b2bb692SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "unlimited"};
1622ee7e38e3SLen Brown 
1623e9257f5fSLen 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};
1624e9257f5fSLen 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};
1625e9257f5fSLen 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};
16260539ba11SLen Brown int slv_pkg_cstate_limits[16] = {PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7};
1627f2642888SLen Brown int amt_pkg_cstate_limits[16] = {PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV};
1628e9257f5fSLen 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};
1629e4085d54SLen 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};
1630d8ebb442SLen 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};
1631ee7e38e3SLen Brown 
1632a2b7b749SLen Brown 
1633a2b7b749SLen Brown static void
1634a2b7b749SLen Brown calculate_tsc_tweak()
1635a2b7b749SLen Brown {
1636a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
1637a2b7b749SLen Brown }
1638a2b7b749SLen Brown 
1639fcd17211SLen Brown static void
1640fcd17211SLen Brown dump_nhm_platform_info(void)
1641103a8feaSLen Brown {
1642103a8feaSLen Brown 	unsigned long long msr;
1643103a8feaSLen Brown 	unsigned int ratio;
1644103a8feaSLen Brown 
1645ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
1646103a8feaSLen Brown 
1647b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
16486574a5d5SLen Brown 
1649103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
1650710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n",
1651103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1652103a8feaSLen Brown 
1653103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
1654710f273bSLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
1655103a8feaSLen Brown 		ratio, bclk, ratio * bclk);
1656103a8feaSLen Brown 
16577ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
1658b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
1659bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
166067920418SLen Brown 
1661fcd17211SLen Brown 	return;
1662fcd17211SLen Brown }
1663fcd17211SLen Brown 
1664fcd17211SLen Brown static void
1665fcd17211SLen Brown dump_hsw_turbo_ratio_limits(void)
1666fcd17211SLen Brown {
1667fcd17211SLen Brown 	unsigned long long msr;
1668fcd17211SLen Brown 	unsigned int ratio;
1669fcd17211SLen Brown 
16707ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
1671fcd17211SLen Brown 
1672b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
1673fcd17211SLen Brown 
1674fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
1675fcd17211SLen Brown 	if (ratio)
1676710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n",
1677fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1678fcd17211SLen Brown 
1679fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
1680fcd17211SLen Brown 	if (ratio)
1681710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n",
1682fcd17211SLen Brown 			ratio, bclk, ratio * bclk);
1683fcd17211SLen Brown 	return;
1684fcd17211SLen Brown }
1685fcd17211SLen Brown 
1686fcd17211SLen Brown static void
1687fcd17211SLen Brown dump_ivt_turbo_ratio_limits(void)
1688fcd17211SLen Brown {
1689fcd17211SLen Brown 	unsigned long long msr;
1690fcd17211SLen Brown 	unsigned int ratio;
16916574a5d5SLen Brown 
16927ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
16936574a5d5SLen Brown 
1694b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
16956574a5d5SLen Brown 
16966574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
16976574a5d5SLen Brown 	if (ratio)
1698710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n",
16996574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17006574a5d5SLen Brown 
17016574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
17026574a5d5SLen Brown 	if (ratio)
1703710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n",
17046574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17056574a5d5SLen Brown 
17066574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
17076574a5d5SLen Brown 	if (ratio)
1708710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n",
17096574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17106574a5d5SLen Brown 
17116574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
17126574a5d5SLen Brown 	if (ratio)
1713710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n",
17146574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17156574a5d5SLen Brown 
17166574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
17176574a5d5SLen Brown 	if (ratio)
1718710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n",
17196574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17206574a5d5SLen Brown 
17216574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
17226574a5d5SLen Brown 	if (ratio)
1723710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n",
17246574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17256574a5d5SLen Brown 
17266574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
17276574a5d5SLen Brown 	if (ratio)
1728710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n",
17296574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
17306574a5d5SLen Brown 
17316574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
17326574a5d5SLen Brown 	if (ratio)
1733710f273bSLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n",
17346574a5d5SLen Brown 			ratio, bclk, ratio * bclk);
1735103a8feaSLen Brown 	return;
1736fcd17211SLen Brown }
173731e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
173831e07522SLen Brown {
173931e07522SLen Brown 
174031e07522SLen Brown 	if (!genuine_intel)
174131e07522SLen Brown 		return 0;
174231e07522SLen Brown 
174331e07522SLen Brown 	switch (model) {
174431e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
174531e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
174631e07522SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
174731e07522SLen Brown 		return 1;
174831e07522SLen Brown 	}
174931e07522SLen Brown 	return 0;
175031e07522SLen Brown }
1751fcd17211SLen Brown 
1752fcd17211SLen Brown static void
175331e07522SLen Brown dump_turbo_ratio_limits(int family, int model)
1754fcd17211SLen Brown {
175531e07522SLen Brown 	unsigned long long msr, core_counts;
175631e07522SLen Brown 	unsigned int ratio, group_size;
1757103a8feaSLen Brown 
17587ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1759b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
17606574a5d5SLen Brown 
176131e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
176231e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
176331e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
176431e07522SLen Brown 	} else {
176531e07522SLen Brown 		core_counts = 0x0807060504030201;
176631e07522SLen Brown 	}
176731e07522SLen Brown 
17686574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
176931e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
17706574a5d5SLen Brown 	if (ratio)
177131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
177231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
17736574a5d5SLen Brown 
17746574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
177531e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
17766574a5d5SLen Brown 	if (ratio)
177731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
177831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
17796574a5d5SLen Brown 
17806574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
178131e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
17826574a5d5SLen Brown 	if (ratio)
178331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
178431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
17856574a5d5SLen Brown 
17866574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
178731e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
17886574a5d5SLen Brown 	if (ratio)
178931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
179031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
17916574a5d5SLen Brown 
1792103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
179331e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
1794103a8feaSLen Brown 	if (ratio)
179531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
179631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1797103a8feaSLen Brown 
1798103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
179931e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
1800103a8feaSLen Brown 	if (ratio)
180131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
180231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1803103a8feaSLen Brown 
1804103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
180531e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
1806103a8feaSLen Brown 	if (ratio)
180731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
180831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1809103a8feaSLen Brown 
1810103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
181131e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
1812103a8feaSLen Brown 	if (ratio)
181331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
181431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
1815fcd17211SLen Brown 	return;
1816fcd17211SLen Brown }
18173a9a941dSLen Brown 
1818fcd17211SLen Brown static void
18190f7887c4SLen Brown dump_atom_turbo_ratio_limits(void)
18200f7887c4SLen Brown {
18210f7887c4SLen Brown 	unsigned long long msr;
18220f7887c4SLen Brown 	unsigned int ratio;
18230f7887c4SLen Brown 
18240f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
18250f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
18260f7887c4SLen Brown 
18270f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
18280f7887c4SLen Brown 	if (ratio)
18290f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n",
18300f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18310f7887c4SLen Brown 
18320f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
18330f7887c4SLen Brown 	if (ratio)
18340f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n",
18350f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18360f7887c4SLen Brown 
18370f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
18380f7887c4SLen Brown 	if (ratio)
18390f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n",
18400f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18410f7887c4SLen Brown 
18420f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
18430f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
18440f7887c4SLen Brown 
18450f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
18460f7887c4SLen Brown 	if (ratio)
18470f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n",
18480f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18490f7887c4SLen Brown 
18500f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
18510f7887c4SLen Brown 	if (ratio)
18520f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n",
18530f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18540f7887c4SLen Brown 
18550f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
18560f7887c4SLen Brown 	if (ratio)
18570f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n",
18580f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18590f7887c4SLen Brown 
18600f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
18610f7887c4SLen Brown 	if (ratio)
18620f7887c4SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n",
18630f7887c4SLen Brown 			ratio, bclk, ratio * bclk);
18640f7887c4SLen Brown }
18650f7887c4SLen Brown 
18660f7887c4SLen Brown static void
1867fb5d4327SDasaratharaman Chandramouli dump_knl_turbo_ratio_limits(void)
1868fb5d4327SDasaratharaman Chandramouli {
1869cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
1870cbf97abaSHubert Chrzaniuk 
1871fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
1872cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
1873cbf97abaSHubert Chrzaniuk 	int i, b_nr;
1874cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
1875cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
1876fb5d4327SDasaratharaman Chandramouli 
1877ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
1878fb5d4327SDasaratharaman Chandramouli 
1879b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n",
1880bfae2052SLen Brown 		base_cpu, msr);
1881fb5d4327SDasaratharaman Chandramouli 
1882fb5d4327SDasaratharaman Chandramouli 	/**
1883fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
1884cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
1885cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
1886fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
1887fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
1888fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
1889fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
1890fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
1891fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
1892fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
1893fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
1894fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
1895fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
1896fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
1897fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
1898fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
1899fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
1900fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
1901fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
1902fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
1903fb5d4327SDasaratharaman Chandramouli 	 */
1904fb5d4327SDasaratharaman Chandramouli 
1905cbf97abaSHubert Chrzaniuk 	b_nr = 0;
1906cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
1907cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
1908cbf97abaSHubert Chrzaniuk 
1909cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
1910fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
1911cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
1912fb5d4327SDasaratharaman Chandramouli 
1913cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
1914cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
1915cbf97abaSHubert Chrzaniuk 		b_nr++;
1916fb5d4327SDasaratharaman Chandramouli 	}
1917cbf97abaSHubert Chrzaniuk 
1918cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
1919cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
1920b7d8c148SLen Brown 			fprintf(outf,
1921710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
1922cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
1923fb5d4327SDasaratharaman Chandramouli }
1924fb5d4327SDasaratharaman Chandramouli 
1925fb5d4327SDasaratharaman Chandramouli static void
1926fcd17211SLen Brown dump_nhm_cst_cfg(void)
1927fcd17211SLen Brown {
1928fcd17211SLen Brown 	unsigned long long msr;
1929fcd17211SLen Brown 
19301df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
1931fcd17211SLen Brown 
1932fcd17211SLen Brown #define SNB_C1_AUTO_UNDEMOTE              (1UL << 27)
1933fcd17211SLen Brown #define SNB_C3_AUTO_UNDEMOTE              (1UL << 28)
1934fcd17211SLen Brown 
19351df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
1936fcd17211SLen Brown 
1937b7d8c148SLen Brown 	fprintf(outf, " (%s%s%s%s%slocked: pkg-cstate-limit=%d: %s)\n",
1938fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
1939fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
1940fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
1941fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
1942fcd17211SLen Brown 		(msr & (1 << 15)) ? "" : "UN",
19436c34f160SLen Brown 		(unsigned int)msr & 0xF,
1944fcd17211SLen Brown 		pkg_cstate_limit_strings[pkg_cstate_limit]);
1945fcd17211SLen Brown 	return;
1946103a8feaSLen Brown }
1947103a8feaSLen Brown 
19486fb3143bSLen Brown static void
19496fb3143bSLen Brown dump_config_tdp(void)
19506fb3143bSLen Brown {
19516fb3143bSLen Brown 	unsigned long long msr;
19526fb3143bSLen Brown 
19536fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
1954b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
1955685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
19566fb3143bSLen Brown 
19576fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
1958b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
19596fb3143bSLen Brown 	if (msr) {
1960685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1961685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1962685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1963685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
19646fb3143bSLen Brown 	}
1965b7d8c148SLen Brown 	fprintf(outf, ")\n");
19666fb3143bSLen Brown 
19676fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
1968b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
19696fb3143bSLen Brown 	if (msr) {
1970685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
1971685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
1972685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
1973685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
19746fb3143bSLen Brown 	}
1975b7d8c148SLen Brown 	fprintf(outf, ")\n");
19766fb3143bSLen Brown 
19776fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
1978b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
19796fb3143bSLen Brown 	if ((msr) & 0x3)
1980b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
1981b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1982b7d8c148SLen Brown 	fprintf(outf, ")\n");
19836fb3143bSLen Brown 
19846fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
1985b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
1986685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
1987b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
1988b7d8c148SLen Brown 	fprintf(outf, ")\n");
19896fb3143bSLen Brown }
19905a63426eSLen Brown 
19915a63426eSLen Brown unsigned int irtl_time_units[] = {1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
19925a63426eSLen Brown 
19935a63426eSLen Brown void print_irtl(void)
19945a63426eSLen Brown {
19955a63426eSLen Brown 	unsigned long long msr;
19965a63426eSLen Brown 
19975a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
19985a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
19995a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20005a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20015a63426eSLen Brown 
20025a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
20035a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
20045a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20055a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20065a63426eSLen Brown 
20075a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
20085a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
20095a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20105a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20115a63426eSLen Brown 
20125a63426eSLen Brown 	if (!do_irtl_hsw)
20135a63426eSLen Brown 		return;
20145a63426eSLen Brown 
20155a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
20165a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
20175a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20185a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20195a63426eSLen Brown 
20205a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
20215a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
20225a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20235a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20245a63426eSLen Brown 
20255a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
20265a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
20275a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
20285a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
20295a63426eSLen Brown 
20305a63426eSLen Brown }
203136229897SLen Brown void free_fd_percpu(void)
203236229897SLen Brown {
203336229897SLen Brown 	int i;
203436229897SLen Brown 
203501a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
203636229897SLen Brown 		if (fd_percpu[i] != 0)
203736229897SLen Brown 			close(fd_percpu[i]);
203836229897SLen Brown 	}
203936229897SLen Brown 
204036229897SLen Brown 	free(fd_percpu);
20416fb3143bSLen Brown }
20426fb3143bSLen Brown 
2043c98d5d94SLen Brown void free_all_buffers(void)
2044103a8feaSLen Brown {
2045c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2046c98d5d94SLen Brown 	cpu_present_set = NULL;
204736229897SLen Brown 	cpu_present_setsize = 0;
2048103a8feaSLen Brown 
2049c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2050c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2051c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2052103a8feaSLen Brown 
2053c98d5d94SLen Brown 	free(thread_even);
2054c98d5d94SLen Brown 	free(core_even);
2055c98d5d94SLen Brown 	free(package_even);
2056103a8feaSLen Brown 
2057c98d5d94SLen Brown 	thread_even = NULL;
2058c98d5d94SLen Brown 	core_even = NULL;
2059c98d5d94SLen Brown 	package_even = NULL;
2060103a8feaSLen Brown 
2061c98d5d94SLen Brown 	free(thread_odd);
2062c98d5d94SLen Brown 	free(core_odd);
2063c98d5d94SLen Brown 	free(package_odd);
2064103a8feaSLen Brown 
2065c98d5d94SLen Brown 	thread_odd = NULL;
2066c98d5d94SLen Brown 	core_odd = NULL;
2067c98d5d94SLen Brown 	package_odd = NULL;
2068103a8feaSLen Brown 
2069c98d5d94SLen Brown 	free(output_buffer);
2070c98d5d94SLen Brown 	output_buffer = NULL;
2071c98d5d94SLen Brown 	outp = NULL;
207236229897SLen Brown 
207336229897SLen Brown 	free_fd_percpu();
2074562a2d37SLen Brown 
2075562a2d37SLen Brown 	free(irq_column_2_cpu);
2076562a2d37SLen Brown 	free(irqs_per_cpu);
2077103a8feaSLen Brown }
2078103a8feaSLen Brown 
207957a42a34SJosh Triplett 
208057a42a34SJosh Triplett /*
208195aebc44SJosh Triplett  * Parse a file containing a single int.
208295aebc44SJosh Triplett  */
208395aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
208495aebc44SJosh Triplett {
208595aebc44SJosh Triplett 	va_list args;
208695aebc44SJosh Triplett 	char path[PATH_MAX];
208795aebc44SJosh Triplett 	FILE *filep;
208895aebc44SJosh Triplett 	int value;
208995aebc44SJosh Triplett 
209095aebc44SJosh Triplett 	va_start(args, fmt);
209195aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
209295aebc44SJosh Triplett 	va_end(args);
209357a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2094b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2095b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
209695aebc44SJosh Triplett 	fclose(filep);
209795aebc44SJosh Triplett 	return value;
209895aebc44SJosh Triplett }
209995aebc44SJosh Triplett 
210095aebc44SJosh Triplett /*
2101e275b388SDasaratharaman Chandramouli  * get_cpu_position_in_core(cpu)
2102e275b388SDasaratharaman Chandramouli  * return the position of the CPU among its HT siblings in the core
2103e275b388SDasaratharaman Chandramouli  * return -1 if the sibling is not in list
2104103a8feaSLen Brown  */
2105e275b388SDasaratharaman Chandramouli int get_cpu_position_in_core(int cpu)
2106103a8feaSLen Brown {
2107e275b388SDasaratharaman Chandramouli 	char path[64];
2108e275b388SDasaratharaman Chandramouli 	FILE *filep;
2109e275b388SDasaratharaman Chandramouli 	int this_cpu;
2110e275b388SDasaratharaman Chandramouli 	char character;
2111e275b388SDasaratharaman Chandramouli 	int i;
2112e275b388SDasaratharaman Chandramouli 
2113e275b388SDasaratharaman Chandramouli 	sprintf(path,
2114e275b388SDasaratharaman Chandramouli 		"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
2115e275b388SDasaratharaman Chandramouli 		cpu);
2116e275b388SDasaratharaman Chandramouli 	filep = fopen(path, "r");
2117e275b388SDasaratharaman Chandramouli 	if (filep == NULL) {
2118e275b388SDasaratharaman Chandramouli 		perror(path);
2119e275b388SDasaratharaman Chandramouli 		exit(1);
2120e275b388SDasaratharaman Chandramouli 	}
2121e275b388SDasaratharaman Chandramouli 
2122e275b388SDasaratharaman Chandramouli 	for (i = 0; i < topo.num_threads_per_core; i++) {
2123e275b388SDasaratharaman Chandramouli 		fscanf(filep, "%d", &this_cpu);
2124e275b388SDasaratharaman Chandramouli 		if (this_cpu == cpu) {
2125e275b388SDasaratharaman Chandramouli 			fclose(filep);
2126e275b388SDasaratharaman Chandramouli 			return i;
2127e275b388SDasaratharaman Chandramouli 		}
2128e275b388SDasaratharaman Chandramouli 
2129e275b388SDasaratharaman Chandramouli 		/* Account for no separator after last thread*/
2130e275b388SDasaratharaman Chandramouli 		if (i != (topo.num_threads_per_core - 1))
2131e275b388SDasaratharaman Chandramouli 			fscanf(filep, "%c", &character);
2132e275b388SDasaratharaman Chandramouli 	}
2133e275b388SDasaratharaman Chandramouli 
2134e275b388SDasaratharaman Chandramouli 	fclose(filep);
2135e275b388SDasaratharaman Chandramouli 	return -1;
2136c98d5d94SLen Brown }
2137103a8feaSLen Brown 
2138c98d5d94SLen Brown /*
2139c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2140c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2141c98d5d94SLen Brown  */
2142c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2143c98d5d94SLen Brown {
214495aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2145103a8feaSLen Brown }
2146103a8feaSLen Brown 
2147103a8feaSLen Brown int get_physical_package_id(int cpu)
2148103a8feaSLen Brown {
214995aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2150103a8feaSLen Brown }
2151103a8feaSLen Brown 
2152103a8feaSLen Brown int get_core_id(int cpu)
2153103a8feaSLen Brown {
215495aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2155103a8feaSLen Brown }
2156103a8feaSLen Brown 
2157c98d5d94SLen Brown int get_num_ht_siblings(int cpu)
2158c98d5d94SLen Brown {
2159c98d5d94SLen Brown 	char path[80];
2160c98d5d94SLen Brown 	FILE *filep;
2161e275b388SDasaratharaman Chandramouli 	int sib1;
2162e275b388SDasaratharaman Chandramouli 	int matches = 0;
2163c98d5d94SLen Brown 	char character;
2164e275b388SDasaratharaman Chandramouli 	char str[100];
2165e275b388SDasaratharaman Chandramouli 	char *ch;
2166c98d5d94SLen Brown 
2167c98d5d94SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", cpu);
216857a42a34SJosh Triplett 	filep = fopen_or_die(path, "r");
2169e275b388SDasaratharaman Chandramouli 
2170103a8feaSLen Brown 	/*
2171c98d5d94SLen Brown 	 * file format:
2172e275b388SDasaratharaman Chandramouli 	 * A ',' separated or '-' separated set of numbers
2173e275b388SDasaratharaman Chandramouli 	 * (eg 1-2 or 1,3,4,5)
2174c98d5d94SLen Brown 	 */
2175e275b388SDasaratharaman Chandramouli 	fscanf(filep, "%d%c\n", &sib1, &character);
2176e275b388SDasaratharaman Chandramouli 	fseek(filep, 0, SEEK_SET);
2177e275b388SDasaratharaman Chandramouli 	fgets(str, 100, filep);
2178e275b388SDasaratharaman Chandramouli 	ch = strchr(str, character);
2179e275b388SDasaratharaman Chandramouli 	while (ch != NULL) {
2180e275b388SDasaratharaman Chandramouli 		matches++;
2181e275b388SDasaratharaman Chandramouli 		ch = strchr(ch+1, character);
2182e275b388SDasaratharaman Chandramouli 	}
2183c98d5d94SLen Brown 
2184c98d5d94SLen Brown 	fclose(filep);
2185e275b388SDasaratharaman Chandramouli 	return matches+1;
2186c98d5d94SLen Brown }
2187c98d5d94SLen Brown 
2188c98d5d94SLen Brown /*
2189c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2190c98d5d94SLen Brown  * skip non-present cpus
2191103a8feaSLen Brown  */
2192103a8feaSLen Brown 
2193c98d5d94SLen Brown int for_all_cpus_2(int (func)(struct thread_data *, struct core_data *,
2194c98d5d94SLen Brown 	struct pkg_data *, struct thread_data *, struct core_data *,
2195c98d5d94SLen Brown 	struct pkg_data *), struct thread_data *thread_base,
2196c98d5d94SLen Brown 	struct core_data *core_base, struct pkg_data *pkg_base,
2197c98d5d94SLen Brown 	struct thread_data *thread_base2, struct core_data *core_base2,
2198c98d5d94SLen Brown 	struct pkg_data *pkg_base2)
2199c98d5d94SLen Brown {
2200c98d5d94SLen Brown 	int retval, pkg_no, core_no, thread_no;
2201c98d5d94SLen Brown 
2202c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
2203c98d5d94SLen Brown 		for (core_no = 0; core_no < topo.num_cores_per_pkg; ++core_no) {
2204c98d5d94SLen Brown 			for (thread_no = 0; thread_no <
2205c98d5d94SLen Brown 				topo.num_threads_per_core; ++thread_no) {
2206c98d5d94SLen Brown 				struct thread_data *t, *t2;
2207c98d5d94SLen Brown 				struct core_data *c, *c2;
2208c98d5d94SLen Brown 				struct pkg_data *p, *p2;
2209c98d5d94SLen Brown 
2210c98d5d94SLen Brown 				t = GET_THREAD(thread_base, thread_no, core_no, pkg_no);
2211c98d5d94SLen Brown 
2212c98d5d94SLen Brown 				if (cpu_is_not_present(t->cpu_id))
2213c98d5d94SLen Brown 					continue;
2214c98d5d94SLen Brown 
2215c98d5d94SLen Brown 				t2 = GET_THREAD(thread_base2, thread_no, core_no, pkg_no);
2216c98d5d94SLen Brown 
2217c98d5d94SLen Brown 				c = GET_CORE(core_base, core_no, pkg_no);
2218c98d5d94SLen Brown 				c2 = GET_CORE(core_base2, core_no, pkg_no);
2219c98d5d94SLen Brown 
2220c98d5d94SLen Brown 				p = GET_PKG(pkg_base, pkg_no);
2221c98d5d94SLen Brown 				p2 = GET_PKG(pkg_base2, pkg_no);
2222c98d5d94SLen Brown 
2223c98d5d94SLen Brown 				retval = func(t, c, p, t2, c2, p2);
2224c98d5d94SLen Brown 				if (retval)
2225c98d5d94SLen Brown 					return retval;
2226c98d5d94SLen Brown 			}
2227c98d5d94SLen Brown 		}
2228c98d5d94SLen Brown 	}
2229c98d5d94SLen Brown 	return 0;
2230c98d5d94SLen Brown }
2231c98d5d94SLen Brown 
2232c98d5d94SLen Brown /*
2233c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
2234c98d5d94SLen Brown  * return max_cpu number
2235c98d5d94SLen Brown  */
2236c98d5d94SLen Brown int for_all_proc_cpus(int (func)(int))
2237103a8feaSLen Brown {
2238103a8feaSLen Brown 	FILE *fp;
2239c98d5d94SLen Brown 	int cpu_num;
2240103a8feaSLen Brown 	int retval;
2241103a8feaSLen Brown 
224257a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
2243103a8feaSLen Brown 
2244103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
2245b2c95d90SJosh Triplett 	if (retval != 0)
2246b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
2247103a8feaSLen Brown 
2248c98d5d94SLen Brown 	while (1) {
2249c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
2250103a8feaSLen Brown 		if (retval != 1)
2251103a8feaSLen Brown 			break;
2252103a8feaSLen Brown 
2253c98d5d94SLen Brown 		retval = func(cpu_num);
2254c98d5d94SLen Brown 		if (retval) {
2255c98d5d94SLen Brown 			fclose(fp);
2256c98d5d94SLen Brown 			return(retval);
2257c98d5d94SLen Brown 		}
2258103a8feaSLen Brown 	}
2259103a8feaSLen Brown 	fclose(fp);
2260c98d5d94SLen Brown 	return 0;
2261103a8feaSLen Brown }
2262103a8feaSLen Brown 
2263103a8feaSLen Brown void re_initialize(void)
2264103a8feaSLen Brown {
2265c98d5d94SLen Brown 	free_all_buffers();
2266c98d5d94SLen Brown 	setup_all_buffers();
2267c98d5d94SLen Brown 	printf("turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
2268103a8feaSLen Brown }
2269103a8feaSLen Brown 
2270c98d5d94SLen Brown 
2271103a8feaSLen Brown /*
2272c98d5d94SLen Brown  * count_cpus()
2273c98d5d94SLen Brown  * remember the last one seen, it will be the max
2274103a8feaSLen Brown  */
2275c98d5d94SLen Brown int count_cpus(int cpu)
2276103a8feaSLen Brown {
2277c98d5d94SLen Brown 	if (topo.max_cpu_num < cpu)
2278c98d5d94SLen Brown 		topo.max_cpu_num = cpu;
2279103a8feaSLen Brown 
2280c98d5d94SLen Brown 	topo.num_cpus += 1;
2281c98d5d94SLen Brown 	return 0;
2282103a8feaSLen Brown }
2283c98d5d94SLen Brown int mark_cpu_present(int cpu)
2284c98d5d94SLen Brown {
2285c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
228615aaa346SLen Brown 	return 0;
2287103a8feaSLen Brown }
2288103a8feaSLen Brown 
2289562a2d37SLen Brown /*
2290562a2d37SLen Brown  * snapshot_proc_interrupts()
2291562a2d37SLen Brown  *
2292562a2d37SLen Brown  * read and record summary of /proc/interrupts
2293562a2d37SLen Brown  *
2294562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
2295562a2d37SLen Brown  */
2296562a2d37SLen Brown int snapshot_proc_interrupts(void)
2297562a2d37SLen Brown {
2298562a2d37SLen Brown 	static FILE *fp;
2299562a2d37SLen Brown 	int column, retval;
2300562a2d37SLen Brown 
2301562a2d37SLen Brown 	if (fp == NULL)
2302562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
2303562a2d37SLen Brown 	else
2304562a2d37SLen Brown 		rewind(fp);
2305562a2d37SLen Brown 
2306562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
2307562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
2308562a2d37SLen Brown 		int cpu_number;
2309562a2d37SLen Brown 
2310562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
2311562a2d37SLen Brown 		if (retval != 1)
2312562a2d37SLen Brown 			break;
2313562a2d37SLen Brown 
2314562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
2315562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
2316562a2d37SLen Brown 			return 1;
2317562a2d37SLen Brown 		}
2318562a2d37SLen Brown 
2319562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
2320562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
2321562a2d37SLen Brown 	}
2322562a2d37SLen Brown 
2323562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
2324562a2d37SLen Brown 	while (1) {
2325562a2d37SLen Brown 		int column;
2326562a2d37SLen Brown 		char buf[64];
2327562a2d37SLen Brown 
2328562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
2329562a2d37SLen Brown 		if (retval != 1)
2330562a2d37SLen Brown 			break;
2331562a2d37SLen Brown 
2332562a2d37SLen Brown 		/* read the count per cpu */
2333562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
2334562a2d37SLen Brown 
2335562a2d37SLen Brown 			int cpu_number, irq_count;
2336562a2d37SLen Brown 
2337562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
2338562a2d37SLen Brown 			if (retval != 1)
2339562a2d37SLen Brown 				break;
2340562a2d37SLen Brown 
2341562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
2342562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
2343562a2d37SLen Brown 
2344562a2d37SLen Brown 		}
2345562a2d37SLen Brown 
2346562a2d37SLen Brown 		while (getc(fp) != '\n')
2347562a2d37SLen Brown 			;	/* flush interrupt description */
2348562a2d37SLen Brown 
2349562a2d37SLen Brown 	}
2350562a2d37SLen Brown 	return 0;
2351562a2d37SLen Brown }
235227d47356SLen Brown /*
2353fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
2354fdf676e5SLen Brown  *
2355fdf676e5SLen Brown  * record snapshot of
2356fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
2357fdf676e5SLen Brown  *
2358fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
2359fdf676e5SLen Brown  */
2360fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
2361fdf676e5SLen Brown {
2362fdf676e5SLen Brown 	FILE *fp;
2363fdf676e5SLen Brown 	int retval;
2364fdf676e5SLen Brown 
2365fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
2366fdf676e5SLen Brown 
2367fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
2368fdf676e5SLen Brown 	if (retval != 1)
2369fdf676e5SLen Brown 		err(1, "GFX rc6");
2370fdf676e5SLen Brown 
2371fdf676e5SLen Brown 	fclose(fp);
2372fdf676e5SLen Brown 
2373fdf676e5SLen Brown 	return 0;
2374fdf676e5SLen Brown }
2375fdf676e5SLen Brown /*
237627d47356SLen Brown  * snapshot_gfx_mhz()
237727d47356SLen Brown  *
237827d47356SLen Brown  * record snapshot of
237927d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
238027d47356SLen Brown  *
238127d47356SLen Brown  * return 1 if config change requires a restart, else return 0
238227d47356SLen Brown  */
238327d47356SLen Brown int snapshot_gfx_mhz(void)
238427d47356SLen Brown {
238527d47356SLen Brown 	static FILE *fp;
238627d47356SLen Brown 	int retval;
238727d47356SLen Brown 
238827d47356SLen Brown 	if (fp == NULL)
238927d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
239027d47356SLen Brown 	else
239127d47356SLen Brown 		rewind(fp);
239227d47356SLen Brown 
239327d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
239427d47356SLen Brown 	if (retval != 1)
239527d47356SLen Brown 		err(1, "GFX MHz");
239627d47356SLen Brown 
239727d47356SLen Brown 	return 0;
239827d47356SLen Brown }
2399562a2d37SLen Brown 
2400562a2d37SLen Brown /*
2401562a2d37SLen Brown  * snapshot /proc and /sys files
2402562a2d37SLen Brown  *
2403562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
2404562a2d37SLen Brown  */
2405562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
2406562a2d37SLen Brown {
2407562a2d37SLen Brown 	if (snapshot_proc_interrupts())
2408562a2d37SLen Brown 		return 1;
2409562a2d37SLen Brown 
2410812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2411fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
2412fdf676e5SLen Brown 
2413812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
241427d47356SLen Brown 		snapshot_gfx_mhz();
241527d47356SLen Brown 
2416562a2d37SLen Brown 	return 0;
2417562a2d37SLen Brown }
2418562a2d37SLen Brown 
2419103a8feaSLen Brown void turbostat_loop()
2420103a8feaSLen Brown {
2421c98d5d94SLen Brown 	int retval;
2422e52966c0SLen Brown 	int restarted = 0;
2423c98d5d94SLen Brown 
2424103a8feaSLen Brown restart:
2425e52966c0SLen Brown 	restarted++;
2426e52966c0SLen Brown 
2427562a2d37SLen Brown 	snapshot_proc_sysfs_files();
2428c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2429d91bb17cSLen Brown 	if (retval < -1) {
2430d91bb17cSLen Brown 		exit(retval);
2431d91bb17cSLen Brown 	} else if (retval == -1) {
2432e52966c0SLen Brown 		if (restarted > 1) {
2433e52966c0SLen Brown 			exit(retval);
2434e52966c0SLen Brown 		}
2435c98d5d94SLen Brown 		re_initialize();
2436c98d5d94SLen Brown 		goto restart;
2437c98d5d94SLen Brown 	}
2438e52966c0SLen Brown 	restarted = 0;
2439103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
2440103a8feaSLen Brown 
2441103a8feaSLen Brown 	while (1) {
2442c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
2443103a8feaSLen Brown 			re_initialize();
2444103a8feaSLen Brown 			goto restart;
2445103a8feaSLen Brown 		}
24462a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2447562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2448562a2d37SLen Brown 			goto restart;
2449c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
2450d91bb17cSLen Brown 		if (retval < -1) {
2451d91bb17cSLen Brown 			exit(retval);
2452d91bb17cSLen Brown 		} else if (retval == -1) {
245315aaa346SLen Brown 			re_initialize();
245415aaa346SLen Brown 			goto restart;
245515aaa346SLen Brown 		}
2456103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
2457103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
2458ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
2459ba3dec99SLen Brown 			re_initialize();
2460ba3dec99SLen Brown 			goto restart;
2461ba3dec99SLen Brown 		}
2462c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
2463c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
2464b7d8c148SLen Brown 		flush_output_stdout();
24652a0609c0SLen Brown 		nanosleep(&interval_ts, NULL);
2466562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
2467562a2d37SLen Brown 			goto restart;
2468c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
2469d91bb17cSLen Brown 		if (retval < -1) {
2470d91bb17cSLen Brown 			exit(retval);
2471d91bb17cSLen Brown 		} else if (retval == -1) {
2472103a8feaSLen Brown 			re_initialize();
2473103a8feaSLen Brown 			goto restart;
2474103a8feaSLen Brown 		}
2475103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
2476103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
2477ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
2478ba3dec99SLen Brown 			re_initialize();
2479ba3dec99SLen Brown 			goto restart;
2480ba3dec99SLen Brown 		}
2481c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
2482c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
2483b7d8c148SLen Brown 		flush_output_stdout();
2484103a8feaSLen Brown 	}
2485103a8feaSLen Brown }
2486103a8feaSLen Brown 
2487103a8feaSLen Brown void check_dev_msr()
2488103a8feaSLen Brown {
2489103a8feaSLen Brown 	struct stat sb;
24907ce7d5deSPrarit Bhargava 	char pathname[32];
2491103a8feaSLen Brown 
24927ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
24937ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
2494a21d38c8SLen Brown  		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
2495d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
2496103a8feaSLen Brown }
2497103a8feaSLen Brown 
249898481e79SLen Brown void check_permissions()
2499103a8feaSLen Brown {
250098481e79SLen Brown 	struct __user_cap_header_struct cap_header_data;
250198481e79SLen Brown 	cap_user_header_t cap_header = &cap_header_data;
250298481e79SLen Brown 	struct __user_cap_data_struct cap_data_data;
250398481e79SLen Brown 	cap_user_data_t cap_data = &cap_data_data;
250498481e79SLen Brown 	extern int capget(cap_user_header_t hdrp, cap_user_data_t datap);
250598481e79SLen Brown 	int do_exit = 0;
25067ce7d5deSPrarit Bhargava 	char pathname[32];
250798481e79SLen Brown 
250898481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
250998481e79SLen Brown 	cap_header->pid = getpid();
251098481e79SLen Brown 	cap_header->version = _LINUX_CAPABILITY_VERSION;
251198481e79SLen Brown 	if (capget(cap_header, cap_data) < 0)
251298481e79SLen Brown 		err(-6, "capget(2) failed");
251398481e79SLen Brown 
251498481e79SLen Brown 	if ((cap_data->effective & (1 << CAP_SYS_RAWIO)) == 0) {
251598481e79SLen Brown 		do_exit++;
251698481e79SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed,"
251798481e79SLen Brown 			" try \"# setcap cap_sys_rawio=ep %s\"", progname);
251898481e79SLen Brown 	}
251998481e79SLen Brown 
252098481e79SLen Brown 	/* test file permissions */
25217ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
25227ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
252398481e79SLen Brown 		do_exit++;
252498481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
252598481e79SLen Brown 	}
252698481e79SLen Brown 
252798481e79SLen Brown 	/* if all else fails, thell them to be root */
252898481e79SLen Brown 	if (do_exit)
2529b2c95d90SJosh Triplett 		if (getuid() != 0)
2530d7899447SLen Brown 			warnx("... or simply run as root");
253198481e79SLen Brown 
253298481e79SLen Brown 	if (do_exit)
253398481e79SLen Brown 		exit(-6);
2534103a8feaSLen Brown }
2535103a8feaSLen Brown 
2536d7899447SLen Brown /*
2537d7899447SLen Brown  * NHM adds support for additional MSRs:
2538d7899447SLen Brown  *
2539d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
2540d7899447SLen Brown  *
2541ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
25421df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
2543d7899447SLen Brown  *
2544cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
2545cf4cbe53SLen Brown  *
2546d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
2547d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
2548d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
2549d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
2550d7899447SLen Brown  *
2551ee7e38e3SLen Brown  * Side effect:
25521df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
255333148d67SLen Brown  * sets has_misc_feature_control
2554d7899447SLen Brown  */
2555ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
2556103a8feaSLen Brown {
2557ee7e38e3SLen Brown 	unsigned long long msr;
255821ed5574SLen Brown 	unsigned int base_ratio;
2559ee7e38e3SLen Brown 	int *pkg_cstate_limits;
2560ee7e38e3SLen Brown 
2561103a8feaSLen Brown 	if (!genuine_intel)
2562103a8feaSLen Brown 		return 0;
2563103a8feaSLen Brown 
2564103a8feaSLen Brown 	if (family != 6)
2565103a8feaSLen Brown 		return 0;
2566103a8feaSLen Brown 
256721ed5574SLen Brown 	bclk = discover_bclk(family, model);
256821ed5574SLen Brown 
2569103a8feaSLen Brown 	switch (model) {
2570869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
2571869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
2572103a8feaSLen Brown 	case 0x1F:	/* Core i7 and i5 Processor - Nehalem */
2573869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
2574869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
2575869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2576869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2577ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
2578ee7e38e3SLen Brown 		break;
2579869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
2580869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
2581869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2582869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2583ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
258433148d67SLen Brown 		has_misc_feature_control = 1;
2585ee7e38e3SLen Brown 		break;
2586869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2587869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2588869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2589869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2590869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2591869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2592869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2593869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2594869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2595869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2596869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2597869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2598ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
259933148d67SLen Brown 		has_misc_feature_control = 1;
2600ee7e38e3SLen Brown 		break;
2601d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
2602d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
260333148d67SLen Brown 		has_misc_feature_control = 1;
2604d8ebb442SLen Brown 		break;
2605869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
2606cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2607869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
2608ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
2609ee7e38e3SLen Brown 		break;
2610869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
2611ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
2612cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
2613ee7e38e3SLen Brown 		break;
2614869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
2615005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2616ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
2617ee7e38e3SLen Brown 		break;
2618869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
2619ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
2620869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
2621e4085d54SLen Brown 		pkg_cstate_limits = bxt_pkg_cstate_limits;
2622e4085d54SLen Brown 		break;
2623103a8feaSLen Brown 	default:
2624103a8feaSLen Brown 		return 0;
2625103a8feaSLen Brown 	}
26261df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2627e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
2628ee7e38e3SLen Brown 
2629ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
263021ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
263121ed5574SLen Brown 
263221ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
263321ed5574SLen Brown 	has_base_hz = 1;
2634ee7e38e3SLen Brown 	return 1;
2635103a8feaSLen Brown }
26360f7887c4SLen Brown /*
2637495c7654SLen Brown  * SLV client has support for unique MSRs:
26380f7887c4SLen Brown  *
26390f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
26400f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
26410f7887c4SLen Brown  */
26420f7887c4SLen Brown 
26430f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
26440f7887c4SLen Brown {
26450f7887c4SLen Brown 	if (!genuine_intel)
26460f7887c4SLen Brown 		return 0;
26470f7887c4SLen Brown 
26480f7887c4SLen Brown 	switch (model) {
26490f7887c4SLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
26500f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MERRIFIELD:
26510f7887c4SLen Brown 	case INTEL_FAM6_ATOM_MOOREFIELD:
26520f7887c4SLen Brown 		return 1;
26530f7887c4SLen Brown 	}
26540f7887c4SLen Brown 	return 0;
26550f7887c4SLen Brown }
26567170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
26577170a374SLen Brown {
26587170a374SLen Brown 
26597170a374SLen Brown 	if (!genuine_intel)
26607170a374SLen Brown 		return 0;
26617170a374SLen Brown 
26627170a374SLen Brown 	switch (model) {
26637170a374SLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:
26647170a374SLen Brown 		return 1;
26657170a374SLen Brown 	}
26667170a374SLen Brown 	return 0;
26677170a374SLen Brown }
2668ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
2669ade0ebacSLen Brown {
2670ade0ebacSLen Brown 
2671ade0ebacSLen Brown 	if (!genuine_intel)
2672ade0ebacSLen Brown 		return 0;
2673ade0ebacSLen Brown 
2674ade0ebacSLen Brown 	switch (model) {
2675ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
2676ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:
2677ade0ebacSLen Brown 		return 1;
2678ade0ebacSLen Brown 	}
2679ade0ebacSLen Brown 	return 0;
2680ade0ebacSLen Brown }
268134c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
268234c76197SLen Brown {
268334c76197SLen Brown 
268434c76197SLen Brown 	if (!genuine_intel)
268534c76197SLen Brown 		return 0;
268634c76197SLen Brown 
268734c76197SLen Brown 	switch (model) {
268834c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
268934c76197SLen Brown 		return 1;
269034c76197SLen Brown 	}
269134c76197SLen Brown 	return 0;
269234c76197SLen Brown }
26930f7887c4SLen Brown 
269431e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
2695d7899447SLen Brown {
26960f7887c4SLen Brown 	if (has_slv_msrs(family, model))
26970f7887c4SLen Brown 		return 0;
26980f7887c4SLen Brown 
2699d7899447SLen Brown 	switch (model) {
2700d7899447SLen Brown 	/* Nehalem compatible, but do not include turbo-ratio limit support */
2701869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
2702869ce69eSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
2703869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
2704005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2705d7899447SLen Brown 		return 0;
2706d7899447SLen Brown 	default:
2707d7899447SLen Brown 		return 1;
2708d7899447SLen Brown 	}
2709d7899447SLen Brown }
27100f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
27110f7887c4SLen Brown {
27120f7887c4SLen Brown 	if (has_slv_msrs(family, model))
27130f7887c4SLen Brown 		return 1;
27140f7887c4SLen Brown 
27150f7887c4SLen Brown 	return 0;
27160f7887c4SLen Brown }
27176574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
27186574a5d5SLen Brown {
27196574a5d5SLen Brown 	if (!genuine_intel)
27206574a5d5SLen Brown 		return 0;
27216574a5d5SLen Brown 
27226574a5d5SLen Brown 	if (family != 6)
27236574a5d5SLen Brown 		return 0;
27246574a5d5SLen Brown 
27256574a5d5SLen Brown 	switch (model) {
2726869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
2727869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
27286574a5d5SLen Brown 		return 1;
27296574a5d5SLen Brown 	default:
27306574a5d5SLen Brown 		return 0;
27316574a5d5SLen Brown 	}
27326574a5d5SLen Brown }
2733fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
2734fcd17211SLen Brown {
2735fcd17211SLen Brown 	if (!genuine_intel)
2736fcd17211SLen Brown 		return 0;
2737fcd17211SLen Brown 
2738fcd17211SLen Brown 	if (family != 6)
2739fcd17211SLen Brown 		return 0;
2740fcd17211SLen Brown 
2741fcd17211SLen Brown 	switch (model) {
2742869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
2743fcd17211SLen Brown 		return 1;
2744fcd17211SLen Brown 	default:
2745fcd17211SLen Brown 		return 0;
2746fcd17211SLen Brown 	}
2747fcd17211SLen Brown }
2748fcd17211SLen Brown 
2749fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
2750fb5d4327SDasaratharaman Chandramouli {
2751fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
2752fb5d4327SDasaratharaman Chandramouli 		return 0;
2753fb5d4327SDasaratharaman Chandramouli 
2754fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
2755fb5d4327SDasaratharaman Chandramouli 		return 0;
2756fb5d4327SDasaratharaman Chandramouli 
2757fb5d4327SDasaratharaman Chandramouli 	switch (model) {
2758869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2759005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
2760fb5d4327SDasaratharaman Chandramouli 		return 1;
2761fb5d4327SDasaratharaman Chandramouli 	default:
2762fb5d4327SDasaratharaman Chandramouli 		return 0;
2763fb5d4327SDasaratharaman Chandramouli 	}
2764fb5d4327SDasaratharaman Chandramouli }
276531e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
276631e07522SLen Brown {
276731e07522SLen Brown 	if (!genuine_intel)
276831e07522SLen Brown 		return 0;
276931e07522SLen Brown 
277031e07522SLen Brown 	if (family != 6)
277131e07522SLen Brown 		return 0;
277231e07522SLen Brown 
277331e07522SLen Brown 	switch (model) {
277431e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
277531e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
277631e07522SLen Brown 		return 1;
277731e07522SLen Brown 	default:
277831e07522SLen Brown 		return 0;
277931e07522SLen Brown 	}
278031e07522SLen Brown }
27816fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
27826fb3143bSLen Brown {
27836fb3143bSLen Brown 	if (!genuine_intel)
27846fb3143bSLen Brown 		return 0;
27856fb3143bSLen Brown 
27866fb3143bSLen Brown 	if (family != 6)
27876fb3143bSLen Brown 		return 0;
27886fb3143bSLen Brown 
27896fb3143bSLen Brown 	switch (model) {
2790869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
2791869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
2792869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
2793869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
2794869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
2795869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
2796869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
2797869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
2798869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
2799869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
2800869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
2801869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
2802869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
2803869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
28046fb3143bSLen Brown 
2805869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
2806005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
28076fb3143bSLen Brown 		return 1;
28086fb3143bSLen Brown 	default:
28096fb3143bSLen Brown 		return 0;
28106fb3143bSLen Brown 	}
28116fb3143bSLen Brown }
28126fb3143bSLen Brown 
2813fcd17211SLen Brown static void
28141b69317dSColin Ian King dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
2815fcd17211SLen Brown {
2816fcd17211SLen Brown 	if (!do_nhm_platform_info)
2817fcd17211SLen Brown 		return;
2818fcd17211SLen Brown 
2819fcd17211SLen Brown 	dump_nhm_platform_info();
2820fcd17211SLen Brown 
2821fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
2822fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
2823fcd17211SLen Brown 
2824fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
2825fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
2826fcd17211SLen Brown 
282731e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
282831e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
2829fcd17211SLen Brown 
28300f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
28310f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
28320f7887c4SLen Brown 
2833fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
2834fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
2835fb5d4327SDasaratharaman Chandramouli 
28366fb3143bSLen Brown 	if (has_config_tdp(family, model))
28376fb3143bSLen Brown 		dump_config_tdp();
28386fb3143bSLen Brown 
2839fcd17211SLen Brown 	dump_nhm_cst_cfg();
2840fcd17211SLen Brown }
2841fcd17211SLen Brown 
2842*41618e63SLen Brown static void
2843*41618e63SLen Brown dump_sysfs_cstate_config(void)
2844*41618e63SLen Brown {
2845*41618e63SLen Brown 	char path[64];
2846*41618e63SLen Brown 	char name_buf[16];
2847*41618e63SLen Brown 	char desc[64];
2848*41618e63SLen Brown 	FILE *input;
2849*41618e63SLen Brown 	int state;
2850*41618e63SLen Brown 	char *sp;
2851*41618e63SLen Brown 
2852*41618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
2853*41618e63SLen Brown 		return;
2854*41618e63SLen Brown 
2855*41618e63SLen Brown 	for (state = 0; state < 10; ++state) {
2856*41618e63SLen Brown 
2857*41618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
2858*41618e63SLen Brown 			base_cpu, state);
2859*41618e63SLen Brown 		input = fopen(path, "r");
2860*41618e63SLen Brown 		if (input == NULL)
2861*41618e63SLen Brown 			continue;
2862*41618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
2863*41618e63SLen Brown 
2864*41618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
2865*41618e63SLen Brown 		sp = strchr(name_buf, '-');
2866*41618e63SLen Brown 		if (!sp)
2867*41618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
2868*41618e63SLen Brown 		*sp = '\0';
2869*41618e63SLen Brown 
2870*41618e63SLen Brown 		fclose(input);
2871*41618e63SLen Brown 
2872*41618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc",
2873*41618e63SLen Brown 			base_cpu, state);
2874*41618e63SLen Brown 		input = fopen(path, "r");
2875*41618e63SLen Brown 		if (input == NULL)
2876*41618e63SLen Brown 			continue;
2877*41618e63SLen Brown 		fgets(desc, sizeof(desc), input);
2878*41618e63SLen Brown 
2879*41618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
2880*41618e63SLen Brown 		fclose(input);
2881*41618e63SLen Brown 	}
2882*41618e63SLen Brown }
2883*41618e63SLen Brown 
28846574a5d5SLen Brown 
2885889facbeSLen Brown /*
2886889facbeSLen Brown  * print_epb()
2887889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
2888889facbeSLen Brown  */
2889889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2890889facbeSLen Brown {
2891889facbeSLen Brown 	unsigned long long msr;
2892889facbeSLen Brown 	char *epb_string;
2893889facbeSLen Brown 	int cpu;
2894889facbeSLen Brown 
2895889facbeSLen Brown 	if (!has_epb)
2896889facbeSLen Brown 		return 0;
2897889facbeSLen Brown 
2898889facbeSLen Brown 	cpu = t->cpu_id;
2899889facbeSLen Brown 
2900889facbeSLen Brown 	/* EPB is per-package */
2901889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2902889facbeSLen Brown 		return 0;
2903889facbeSLen Brown 
2904889facbeSLen Brown 	if (cpu_migrate(cpu)) {
2905b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
2906889facbeSLen Brown 		return -1;
2907889facbeSLen Brown 	}
2908889facbeSLen Brown 
2909889facbeSLen Brown 	if (get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr))
2910889facbeSLen Brown 		return 0;
2911889facbeSLen Brown 
2912e9be7dd6SLen Brown 	switch (msr & 0xF) {
2913889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
2914889facbeSLen Brown 		epb_string = "performance";
2915889facbeSLen Brown 		break;
2916889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
2917889facbeSLen Brown 		epb_string = "balanced";
2918889facbeSLen Brown 		break;
2919889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
2920889facbeSLen Brown 		epb_string = "powersave";
2921889facbeSLen Brown 		break;
2922889facbeSLen Brown 	default:
2923889facbeSLen Brown 		epb_string = "custom";
2924889facbeSLen Brown 		break;
2925889facbeSLen Brown 	}
2926b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_ENERGY_PERF_BIAS: 0x%08llx (%s)\n", cpu, msr, epb_string);
2927889facbeSLen Brown 
2928889facbeSLen Brown 	return 0;
2929889facbeSLen Brown }
29307f5c258eSLen Brown /*
29317f5c258eSLen Brown  * print_hwp()
29327f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
29337f5c258eSLen Brown  */
29347f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
29357f5c258eSLen Brown {
29367f5c258eSLen Brown 	unsigned long long msr;
29377f5c258eSLen Brown 	int cpu;
29387f5c258eSLen Brown 
29397f5c258eSLen Brown 	if (!has_hwp)
29407f5c258eSLen Brown 		return 0;
29417f5c258eSLen Brown 
29427f5c258eSLen Brown 	cpu = t->cpu_id;
29437f5c258eSLen Brown 
29447f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
29457f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
29467f5c258eSLen Brown 		return 0;
29477f5c258eSLen Brown 
29487f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
2949b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
29507f5c258eSLen Brown 		return -1;
29517f5c258eSLen Brown 	}
29527f5c258eSLen Brown 
29537f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
29547f5c258eSLen Brown 		return 0;
29557f5c258eSLen Brown 
2956b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n",
29577f5c258eSLen Brown 		cpu, msr, (msr & (1 << 0)) ? "" : "No-");
29587f5c258eSLen Brown 
29597f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
29607f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
29617f5c258eSLen Brown 		return 0;
29627f5c258eSLen Brown 
29637f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
29647f5c258eSLen Brown 		return 0;
29657f5c258eSLen Brown 
2966b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
29677f5c258eSLen Brown 			"(high 0x%x guar 0x%x eff 0x%x low 0x%x)\n",
29687f5c258eSLen Brown 			cpu, msr,
29697f5c258eSLen Brown 			(unsigned int)HWP_HIGHEST_PERF(msr),
29707f5c258eSLen Brown 			(unsigned int)HWP_GUARANTEED_PERF(msr),
29717f5c258eSLen Brown 			(unsigned int)HWP_MOSTEFFICIENT_PERF(msr),
29727f5c258eSLen Brown 			(unsigned int)HWP_LOWEST_PERF(msr));
29737f5c258eSLen Brown 
29747f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
29757f5c258eSLen Brown 		return 0;
29767f5c258eSLen Brown 
2977b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
29787f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x pkg 0x%x)\n",
29797f5c258eSLen Brown 			cpu, msr,
29807f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
29817f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
29827f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
29837f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
29847f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3),
29857f5c258eSLen Brown 			(unsigned int)(((msr) >> 42) & 0x1));
29867f5c258eSLen Brown 
29877f5c258eSLen Brown 	if (has_hwp_pkg) {
29887f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
29897f5c258eSLen Brown 			return 0;
29907f5c258eSLen Brown 
2991b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
29927f5c258eSLen Brown 			"(min 0x%x max 0x%x des 0x%x epp 0x%x window 0x%x)\n",
29937f5c258eSLen Brown 			cpu, msr,
29947f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
29957f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
29967f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
29977f5c258eSLen Brown 			(unsigned int)(((msr) >> 24) & 0xff),
29987f5c258eSLen Brown 			(unsigned int)(((msr) >> 32) & 0xff3));
29997f5c258eSLen Brown 	}
30007f5c258eSLen Brown 	if (has_hwp_notify) {
30017f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
30027f5c258eSLen Brown 			return 0;
30037f5c258eSLen Brown 
3004b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
30057f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
30067f5c258eSLen Brown 			cpu, msr,
30077f5c258eSLen Brown 			((msr) & 0x1) ? "EN" : "Dis",
30087f5c258eSLen Brown 			((msr) & 0x2) ? "EN" : "Dis");
30097f5c258eSLen Brown 	}
30107f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
30117f5c258eSLen Brown 		return 0;
30127f5c258eSLen Brown 
3013b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
30147f5c258eSLen Brown 			"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
30157f5c258eSLen Brown 			cpu, msr,
30167f5c258eSLen Brown 			((msr) & 0x1) ? "" : "No-",
30177f5c258eSLen Brown 			((msr) & 0x2) ? "" : "No-");
3018889facbeSLen Brown 
3019889facbeSLen Brown 	return 0;
3020889facbeSLen Brown }
3021889facbeSLen Brown 
30223a9a941dSLen Brown /*
30233a9a941dSLen Brown  * print_perf_limit()
30243a9a941dSLen Brown  */
30253a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
30263a9a941dSLen Brown {
30273a9a941dSLen Brown 	unsigned long long msr;
30283a9a941dSLen Brown 	int cpu;
30293a9a941dSLen Brown 
30303a9a941dSLen Brown 	cpu = t->cpu_id;
30313a9a941dSLen Brown 
30323a9a941dSLen Brown 	/* per-package */
30333a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
30343a9a941dSLen Brown 		return 0;
30353a9a941dSLen Brown 
30363a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
3037b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
30383a9a941dSLen Brown 		return -1;
30393a9a941dSLen Brown 	}
30403a9a941dSLen Brown 
30413a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
30423a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
3043b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3044b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
3045e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
30463a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
3047e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
3048e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
3049e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
3050e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
3051e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
3052e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
3053e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
3054e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
3055e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
3056e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
3057e33cbe85SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
3058e33cbe85SLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "");
3059b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
3060e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
30613a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
3062e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
3063e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
3064e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
3065e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
3066e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
3067e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
3068e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
3069e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
3070e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
3071e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
3072e33cbe85SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
3073e33cbe85SLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "");
30743a9a941dSLen Brown 
30753a9a941dSLen Brown 	}
30763a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
30773a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
3078b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3079b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
30803a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
30813a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
30823a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
30833a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
30843a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
30853a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
30863a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
30873a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3088b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
30893a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
30903a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
30913a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
30923a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
30933a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
30943a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
30953a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
30963a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
30973a9a941dSLen Brown 	}
30983a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
30993a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
3100b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
3101b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
31023a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
31033a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
31043a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
31053a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
31063a9a941dSLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
31073a9a941dSLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "");
3108b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
31093a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
31103a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
31113a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
31123a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
31133a9a941dSLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
31143a9a941dSLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "");
31153a9a941dSLen Brown 	}
31163a9a941dSLen Brown 	return 0;
31173a9a941dSLen Brown }
31183a9a941dSLen Brown 
3119889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
3120889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F /* 6 bit time granularity */
3121889facbeSLen Brown 
31221b69317dSColin Ian King double get_tdp(unsigned int model)
3123144b44b1SLen Brown {
3124144b44b1SLen Brown 	unsigned long long msr;
3125144b44b1SLen Brown 
3126144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
31277ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
3128144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
3129144b44b1SLen Brown 
3130144b44b1SLen Brown 	switch (model) {
3131869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:
3132869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:
3133144b44b1SLen Brown 		return 30.0;
3134144b44b1SLen Brown 	default:
3135144b44b1SLen Brown 		return 135.0;
3136144b44b1SLen Brown 	}
3137144b44b1SLen Brown }
3138144b44b1SLen Brown 
313940ee8e3bSAndrey Semin /*
314040ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
314140ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
314240ee8e3bSAndrey Semin  */
314340ee8e3bSAndrey Semin static double
314440ee8e3bSAndrey Semin rapl_dram_energy_units_probe(int  model, double rapl_energy_units)
314540ee8e3bSAndrey Semin {
314640ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
314740ee8e3bSAndrey Semin 
314840ee8e3bSAndrey Semin 	switch (model) {
3149869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3150869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3151869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3152869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3153005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
315440ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
315540ee8e3bSAndrey Semin 	default:
315640ee8e3bSAndrey Semin 		return (rapl_energy_units);
315740ee8e3bSAndrey Semin 	}
315840ee8e3bSAndrey Semin }
315940ee8e3bSAndrey Semin 
3160144b44b1SLen Brown 
3161889facbeSLen Brown /*
3162889facbeSLen Brown  * rapl_probe()
3163889facbeSLen Brown  *
3164144b44b1SLen Brown  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
3165889facbeSLen Brown  */
3166889facbeSLen Brown void rapl_probe(unsigned int family, unsigned int model)
3167889facbeSLen Brown {
3168889facbeSLen Brown 	unsigned long long msr;
3169144b44b1SLen Brown 	unsigned int time_unit;
3170889facbeSLen Brown 	double tdp;
3171889facbeSLen Brown 
3172889facbeSLen Brown 	if (!genuine_intel)
3173889facbeSLen Brown 		return;
3174889facbeSLen Brown 
3175889facbeSLen Brown 	if (family != 6)
3176889facbeSLen Brown 		return;
3177889facbeSLen Brown 
3178889facbeSLen Brown 	switch (model) {
3179869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3180869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
3181869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3182869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3183869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3184869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3185869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3186144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
3187812db3f7SLen Brown 		if (rapl_joules) {
3188812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3189812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3190812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
3191812db3f7SLen Brown 		} else {
3192812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3193812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3194812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
3195812db3f7SLen Brown 		}
3196889facbeSLen Brown 		break;
3197869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3198ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3199e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
3200812db3f7SLen Brown 		if (rapl_joules)
3201812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3202812db3f7SLen Brown 		else
3203812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3204e4085d54SLen Brown 		break;
3205869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3206869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3207869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3208869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
32090b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3210812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3211812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3212812db3f7SLen Brown 		if (rapl_joules) {
3213812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3214812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3215812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3216812db3f7SLen Brown 		} else {
3217812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3218812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3219812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3220812db3f7SLen Brown 		}
32210b2bb692SLen Brown 		break;
3222869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
3223869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3224869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3225869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3226869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3227005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
32280b2bb692SLen Brown 		do_rapl = RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
3229812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3230812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3231812db3f7SLen Brown 		if (rapl_joules) {
3232812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3233812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3234812db3f7SLen Brown 		} else {
3235812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3236812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3237812db3f7SLen Brown 		}
3238e6f9bb3cSLen Brown 		break;
3239869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3240869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
32410b2bb692SLen 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;
3242812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3243812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3244812db3f7SLen Brown 		if (rapl_joules) {
3245812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3246812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3247812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3248812db3f7SLen Brown 		} else {
3249812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3250812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3251812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3252812db3f7SLen Brown 		}
3253144b44b1SLen Brown 		break;
3254869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3255869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3256144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
3257812db3f7SLen Brown 		if (rapl_joules) {
3258812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3259812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3260812db3f7SLen Brown 		} else {
3261812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3262812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3263812db3f7SLen Brown 		}
3264889facbeSLen Brown 		break;
3265869ce69eSLen Brown 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
32660f644909SJacob 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;
3267812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
3268812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
3269812db3f7SLen Brown 		if (rapl_joules) {
3270812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
3271812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
3272812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
3273812db3f7SLen Brown 		} else {
3274812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
3275812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
3276812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
3277812db3f7SLen Brown 		}
32780f644909SJacob Pan 		break;
3279889facbeSLen Brown 	default:
3280889facbeSLen Brown 		return;
3281889facbeSLen Brown 	}
3282889facbeSLen Brown 
3283889facbeSLen Brown 	/* units on package 0, verify later other packages match */
32847ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
3285889facbeSLen Brown 		return;
3286889facbeSLen Brown 
3287889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
3288869ce69eSLen Brown 	if (model == INTEL_FAM6_ATOM_SILVERMONT1)
3289144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
3290144b44b1SLen Brown 	else
3291889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
3292889facbeSLen Brown 
329340ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
329440ee8e3bSAndrey Semin 
3295144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
3296144b44b1SLen Brown 	if (time_unit == 0)
3297144b44b1SLen Brown 		time_unit = 0xA;
3298889facbeSLen Brown 
3299144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
3300144b44b1SLen Brown 
3301144b44b1SLen Brown 	tdp = get_tdp(model);
3302889facbeSLen Brown 
3303889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
330496e47158SLen Brown 	if (!quiet)
3305b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
3306889facbeSLen Brown 
3307889facbeSLen Brown 	return;
3308889facbeSLen Brown }
3309889facbeSLen Brown 
33101b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
33113a9a941dSLen Brown {
33123a9a941dSLen Brown 	if (!genuine_intel)
33133a9a941dSLen Brown 		return;
33143a9a941dSLen Brown 
33153a9a941dSLen Brown 	if (family != 6)
33163a9a941dSLen Brown 		return;
33173a9a941dSLen Brown 
33183a9a941dSLen Brown 	switch (model) {
3319869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3320869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3321869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
33223a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
3323869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
33243a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
33253a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
33263a9a941dSLen Brown 	default:
33273a9a941dSLen Brown 		return;
33283a9a941dSLen Brown 	}
33293a9a941dSLen Brown }
33303a9a941dSLen Brown 
3331889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3332889facbeSLen Brown {
3333889facbeSLen Brown 	unsigned long long msr;
3334889facbeSLen Brown 	unsigned int dts;
3335889facbeSLen Brown 	int cpu;
3336889facbeSLen Brown 
3337889facbeSLen Brown 	if (!(do_dts || do_ptm))
3338889facbeSLen Brown 		return 0;
3339889facbeSLen Brown 
3340889facbeSLen Brown 	cpu = t->cpu_id;
3341889facbeSLen Brown 
3342889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
3343889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
3344889facbeSLen Brown 		return 0;
3345889facbeSLen Brown 
3346889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3347b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3348889facbeSLen Brown 		return -1;
3349889facbeSLen Brown 	}
3350889facbeSLen Brown 
3351889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
3352889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
3353889facbeSLen Brown 			return 0;
3354889facbeSLen Brown 
3355889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3356b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n",
3357889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts);
3358889facbeSLen Brown 
3359889facbeSLen Brown #ifdef	THERM_DEBUG
3360889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
3361889facbeSLen Brown 			return 0;
3362889facbeSLen Brown 
3363889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3364889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3365b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3366889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3367889facbeSLen Brown #endif
3368889facbeSLen Brown 	}
3369889facbeSLen Brown 
3370889facbeSLen Brown 
3371889facbeSLen Brown 	if (do_dts) {
3372889facbeSLen Brown 		unsigned int resolution;
3373889facbeSLen Brown 
3374889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
3375889facbeSLen Brown 			return 0;
3376889facbeSLen Brown 
3377889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3378889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
3379b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
3380889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, resolution);
3381889facbeSLen Brown 
3382889facbeSLen Brown #ifdef THERM_DEBUG
3383889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
3384889facbeSLen Brown 			return 0;
3385889facbeSLen Brown 
3386889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
3387889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
3388b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
3389889facbeSLen Brown 			cpu, msr, tcc_activation_temp - dts, tcc_activation_temp - dts2);
3390889facbeSLen Brown #endif
3391889facbeSLen Brown 	}
3392889facbeSLen Brown 
3393889facbeSLen Brown 	return 0;
3394889facbeSLen Brown }
3395889facbeSLen Brown 
3396889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
3397889facbeSLen Brown {
3398b7d8c148SLen Brown 	fprintf(outf, "cpu%d: %s: %sabled (%f Watts, %f sec, clamp %sabled)\n",
3399889facbeSLen Brown 		cpu, label,
3400889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
3401889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
3402889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3)/4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
3403889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
3404889facbeSLen Brown 
3405889facbeSLen Brown 	return;
3406889facbeSLen Brown }
3407889facbeSLen Brown 
3408889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3409889facbeSLen Brown {
3410889facbeSLen Brown 	unsigned long long msr;
3411889facbeSLen Brown 	int cpu;
3412889facbeSLen Brown 
3413889facbeSLen Brown 	if (!do_rapl)
3414889facbeSLen Brown 		return 0;
3415889facbeSLen Brown 
3416889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
3417889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3418889facbeSLen Brown 		return 0;
3419889facbeSLen Brown 
3420889facbeSLen Brown 	cpu = t->cpu_id;
3421889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3422b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3423889facbeSLen Brown 		return -1;
3424889facbeSLen Brown 	}
3425889facbeSLen Brown 
3426889facbeSLen Brown 	if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
3427889facbeSLen Brown 		return -1;
3428889facbeSLen Brown 
342996e47158SLen Brown 	fprintf(outf, "cpu%d: MSR_RAPL_POWER_UNIT: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr,
3430144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
343196e47158SLen Brown 
3432144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
3433144b44b1SLen Brown 
3434889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
3435889facbeSLen Brown                 	return -5;
3436889facbeSLen Brown 
3437889facbeSLen Brown 
3438b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3439889facbeSLen Brown 			cpu, msr,
3440889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3441889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3442889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3443889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
3444889facbeSLen Brown 
3445144b44b1SLen Brown 	}
3446144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
3447144b44b1SLen Brown 
3448889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
3449889facbeSLen Brown 			return -9;
3450889facbeSLen Brown 
3451b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
3452889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
3453889facbeSLen Brown 
3454889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
3455b7d8c148SLen Brown 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%f Watts, %f* sec, clamp %sabled)\n",
3456889facbeSLen Brown 			cpu,
3457889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
3458889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
3459889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3)/4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
3460889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
3461889facbeSLen Brown 	}
3462889facbeSLen Brown 
34630b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
3464889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
3465889facbeSLen Brown                 	return -6;
3466889facbeSLen Brown 
3467b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
3468889facbeSLen Brown 			cpu, msr,
3469889facbeSLen Brown 			((msr >>  0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3470889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3471889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
3472889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
34730b2bb692SLen Brown 	}
34740b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
3475889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
3476889facbeSLen Brown 			return -9;
3477b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
3478889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3479889facbeSLen Brown 
3480889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
3481889facbeSLen Brown 	}
3482144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
3483889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
3484889facbeSLen Brown 			return -7;
3485889facbeSLen Brown 
3486b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
3487144b44b1SLen Brown 	}
34889148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
3489889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
3490889facbeSLen Brown 			return -9;
3491b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
3492889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3493889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
3494889facbeSLen Brown 	}
3495889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
3496889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
3497889facbeSLen Brown 			return -8;
3498889facbeSLen Brown 
3499b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
3500889facbeSLen Brown 
3501889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
3502889facbeSLen Brown 			return -9;
3503b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
3504889facbeSLen Brown 				cpu, msr, (msr >> 31) & 1 ? "" : "UN");
3505889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
3506889facbeSLen Brown 	}
3507889facbeSLen Brown 	return 0;
3508889facbeSLen Brown }
3509889facbeSLen Brown 
3510d7899447SLen Brown /*
3511d7899447SLen Brown  * SNB adds support for additional MSRs:
3512d7899447SLen Brown  *
3513d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
3514d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
3515d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
3516d7899447SLen Brown  */
3517103a8feaSLen Brown 
3518d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
3519103a8feaSLen Brown {
3520103a8feaSLen Brown 	if (!genuine_intel)
3521103a8feaSLen Brown 		return 0;
3522103a8feaSLen Brown 
3523103a8feaSLen Brown 	switch (model) {
3524869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
3525869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
3526869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3527869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3528869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_CORE:	/* HSW */
3529869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
3530869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3531869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_GT3E:	/* HSW */
3532869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3533869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_GT3E:	/* BDW */
3534869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3535869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_XEON_D:	/* BDX-DE */
3536869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3537869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3538869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3539869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3540869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3541869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3542ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
35435bbac26eSXiaolong Wang 	case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3544103a8feaSLen Brown 		return 1;
3545103a8feaSLen Brown 	}
3546103a8feaSLen Brown 	return 0;
3547103a8feaSLen Brown }
3548103a8feaSLen Brown 
3549d7899447SLen Brown /*
3550d7899447SLen Brown  * HSW adds support for additional MSRs:
3551d7899447SLen Brown  *
3552d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
3553d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
3554d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
35555a63426eSLen Brown  *
35565a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
35575a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
35585a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
35595a63426eSLen Brown  *
3560d7899447SLen Brown  */
3561d7899447SLen Brown int has_hsw_msrs(unsigned int family, unsigned int model)
3562ca58710fSKristen Carlson Accardi {
3563ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
3564ca58710fSKristen Carlson Accardi 		return 0;
3565ca58710fSKristen Carlson Accardi 
3566ca58710fSKristen Carlson Accardi 	switch (model) {
3567869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_ULT:	/* HSW */
3568869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_CORE:	/* BDW */
3569869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3570869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3571869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3572869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3573869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3574ac01ac13SLen Brown 	case INTEL_FAM6_ATOM_GEMINI_LAKE:
3575ca58710fSKristen Carlson Accardi 		return 1;
3576ca58710fSKristen Carlson Accardi 	}
3577ca58710fSKristen Carlson Accardi 	return 0;
3578ca58710fSKristen Carlson Accardi }
3579ca58710fSKristen Carlson Accardi 
35800b2bb692SLen Brown /*
35810b2bb692SLen Brown  * SKL adds support for additional MSRS:
35820b2bb692SLen Brown  *
35830b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
35840b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
35850b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
35860b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
35870b2bb692SLen Brown  */
35880b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
35890b2bb692SLen Brown {
35900b2bb692SLen Brown 	if (!genuine_intel)
35910b2bb692SLen Brown 		return 0;
35920b2bb692SLen Brown 
35930b2bb692SLen Brown 	switch (model) {
3594869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3595869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3596869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3597869ce69eSLen Brown 	case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
35980b2bb692SLen Brown 		return 1;
35990b2bb692SLen Brown 	}
36000b2bb692SLen Brown 	return 0;
36010b2bb692SLen Brown }
36020b2bb692SLen Brown 
3603144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
3604144b44b1SLen Brown {
3605144b44b1SLen Brown 	if (!genuine_intel)
3606144b44b1SLen Brown 		return 0;
3607144b44b1SLen Brown 	switch (model) {
3608869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT1:	/* BYT */
3609869ce69eSLen Brown 	case INTEL_FAM6_ATOM_SILVERMONT2:	/* AVN */
3610144b44b1SLen Brown 		return 1;
3611144b44b1SLen Brown 	}
3612144b44b1SLen Brown 	return 0;
3613144b44b1SLen Brown }
3614144b44b1SLen Brown 
3615fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
3616fb5d4327SDasaratharaman Chandramouli {
3617fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3618fb5d4327SDasaratharaman Chandramouli 		return 0;
3619fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3620869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
3621005c82d6SLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
3622fb5d4327SDasaratharaman Chandramouli 		return 1;
3623fb5d4327SDasaratharaman Chandramouli 	}
3624fb5d4327SDasaratharaman Chandramouli 	return 0;
3625fb5d4327SDasaratharaman Chandramouli }
3626fb5d4327SDasaratharaman Chandramouli 
3627b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
3628b2b34dfeSHubert Chrzaniuk {
3629b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
3630b2b34dfeSHubert Chrzaniuk 		return 1024;
3631b2b34dfeSHubert Chrzaniuk 	return 1;
3632b2b34dfeSHubert Chrzaniuk }
3633b2b34dfeSHubert Chrzaniuk 
3634144b44b1SLen Brown #define SLM_BCLK_FREQS 5
3635144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0};
3636144b44b1SLen Brown 
3637144b44b1SLen Brown double slm_bclk(void)
3638144b44b1SLen Brown {
3639144b44b1SLen Brown 	unsigned long long msr = 3;
3640144b44b1SLen Brown 	unsigned int i;
3641144b44b1SLen Brown 	double freq;
3642144b44b1SLen Brown 
36437ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
3644b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
3645144b44b1SLen Brown 
3646144b44b1SLen Brown 	i = msr & 0xf;
3647144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
3648b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
36490a91e551SColin Ian King 		i = 3;
3650144b44b1SLen Brown 	}
3651144b44b1SLen Brown 	freq = slm_freq_table[i];
3652144b44b1SLen Brown 
365396e47158SLen Brown 	if (!quiet)
3654b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
3655144b44b1SLen Brown 
3656144b44b1SLen Brown 	return freq;
3657144b44b1SLen Brown }
3658144b44b1SLen Brown 
3659103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
3660103a8feaSLen Brown {
3661121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
3662103a8feaSLen Brown 		return 100.00;
3663144b44b1SLen Brown 	else if (is_slm(family, model))
3664144b44b1SLen Brown 		return slm_bclk();
3665103a8feaSLen Brown 	else
3666103a8feaSLen Brown 		return 133.33;
3667103a8feaSLen Brown }
3668103a8feaSLen Brown 
3669889facbeSLen Brown /*
3670889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
3671889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
3672889facbeSLen Brown  * This is usually equal to tjMax.
3673889facbeSLen Brown  *
3674889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
3675889facbeSLen Brown  * but also allow cmdline over-ride with -T.
3676889facbeSLen Brown  *
3677889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
3678889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
3679889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
3680889facbeSLen Brown  */
3681889facbeSLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
3682889facbeSLen Brown {
3683889facbeSLen Brown 	unsigned long long msr;
3684889facbeSLen Brown 	unsigned int target_c_local;
3685889facbeSLen Brown 	int cpu;
3686889facbeSLen Brown 
3687889facbeSLen Brown 	/* tcc_activation_temp is used only for dts or ptm */
3688889facbeSLen Brown 	if (!(do_dts || do_ptm))
3689889facbeSLen Brown 		return 0;
3690889facbeSLen Brown 
3691889facbeSLen Brown 	/* this is a per-package concept */
3692889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
3693889facbeSLen Brown 		return 0;
3694889facbeSLen Brown 
3695889facbeSLen Brown 	cpu = t->cpu_id;
3696889facbeSLen Brown 	if (cpu_migrate(cpu)) {
3697b7d8c148SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
3698889facbeSLen Brown 		return -1;
3699889facbeSLen Brown 	}
3700889facbeSLen Brown 
3701889facbeSLen Brown 	if (tcc_activation_temp_override != 0) {
3702889facbeSLen Brown 		tcc_activation_temp = tcc_activation_temp_override;
3703b7d8c148SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n",
3704889facbeSLen Brown 			cpu, tcc_activation_temp);
3705889facbeSLen Brown 		return 0;
3706889facbeSLen Brown 	}
3707889facbeSLen Brown 
3708889facbeSLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
3709d7899447SLen Brown 	if (!do_nhm_platform_info)
3710889facbeSLen Brown 		goto guess;
3711889facbeSLen Brown 
37127ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
3713889facbeSLen Brown 		goto guess;
3714889facbeSLen Brown 
37153482124aSJean Delvare 	target_c_local = (msr >> 16) & 0xFF;
3716889facbeSLen Brown 
371796e47158SLen Brown 	if (!quiet)
3718b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n",
3719889facbeSLen Brown 			cpu, msr, target_c_local);
3720889facbeSLen Brown 
37213482124aSJean Delvare 	if (!target_c_local)
3722889facbeSLen Brown 		goto guess;
3723889facbeSLen Brown 
3724889facbeSLen Brown 	tcc_activation_temp = target_c_local;
3725889facbeSLen Brown 
3726889facbeSLen Brown 	return 0;
3727889facbeSLen Brown 
3728889facbeSLen Brown guess:
3729889facbeSLen Brown 	tcc_activation_temp = TJMAX_DEFAULT;
3730b7d8c148SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n",
3731889facbeSLen Brown 		cpu, tcc_activation_temp);
3732889facbeSLen Brown 
3733889facbeSLen Brown 	return 0;
3734889facbeSLen Brown }
373569807a63SLen Brown 
3736aa8d8cc7SLen Brown void decode_feature_control_msr(void)
3737aa8d8cc7SLen Brown {
3738aa8d8cc7SLen Brown 	unsigned long long msr;
3739aa8d8cc7SLen Brown 
3740aa8d8cc7SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_FEATURE_CONTROL, &msr))
3741aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
3742aa8d8cc7SLen Brown 			base_cpu, msr,
3743aa8d8cc7SLen Brown 			msr & FEATURE_CONTROL_LOCKED ? "" : "UN-",
3744aa8d8cc7SLen Brown 			msr & (1 << 18) ? "SGX" : "");
3745aa8d8cc7SLen Brown }
3746aa8d8cc7SLen Brown 
374769807a63SLen Brown void decode_misc_enable_msr(void)
374869807a63SLen Brown {
374969807a63SLen Brown 	unsigned long long msr;
375069807a63SLen Brown 
375169807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
3752e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
375369807a63SLen Brown 			base_cpu, msr,
3754e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
3755e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
3756e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "No-" : "",
3757e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
3758e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
375969807a63SLen Brown }
376069807a63SLen Brown 
376133148d67SLen Brown void decode_misc_feature_control(void)
376233148d67SLen Brown {
376333148d67SLen Brown 	unsigned long long msr;
376433148d67SLen Brown 
376533148d67SLen Brown 	if (!has_misc_feature_control)
376633148d67SLen Brown 		return;
376733148d67SLen Brown 
376833148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
376933148d67SLen Brown 		fprintf(outf, "cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
377033148d67SLen Brown 			base_cpu, msr,
377133148d67SLen Brown 			msr & (0 << 0) ? "No-" : "",
377233148d67SLen Brown 			msr & (1 << 0) ? "No-" : "",
377333148d67SLen Brown 			msr & (2 << 0) ? "No-" : "",
377433148d67SLen Brown 			msr & (3 << 0) ? "No-" : "");
377533148d67SLen Brown }
3776f0057310SLen Brown /*
3777f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
3778f0057310SLen Brown  *
3779f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
3780f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
3781f0057310SLen Brown  * bit[1] less so...
3782f0057310SLen Brown  */
3783f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
3784f0057310SLen Brown {
3785f0057310SLen Brown 	unsigned long long msr;
3786f0057310SLen Brown 
3787f0057310SLen Brown 	if (!do_nhm_platform_info)
3788f0057310SLen Brown 		return;
3789f0057310SLen Brown 
3790cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
3791cf4cbe53SLen Brown 		return;
3792cf4cbe53SLen Brown 
3793f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
3794ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
3795f0057310SLen Brown 			base_cpu, msr,
3796f0057310SLen Brown 			msr & (1 << 0) ? "DIS" : "EN",
3797ddadb8adSSrinivas Pandruvada 			msr & (1 << 1) ? "EN" : "DIS",
3798ddadb8adSSrinivas Pandruvada 			msr & (1 << 8) ? "EN" : "DIS");
3799f0057310SLen Brown }
380071616c8eSLen Brown /*
380171616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
380271616c8eSLen Brown  *
380371616c8eSLen Brown  * This MSRs are present on Silvermont processors,
380471616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
380571616c8eSLen Brown  */
380671616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
380771616c8eSLen Brown {
380871616c8eSLen Brown 	unsigned long long msr;
380971616c8eSLen Brown 
381071616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
381171616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
381271616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
381371616c8eSLen Brown 
381471616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
381571616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
381671616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
381771616c8eSLen Brown }
38187f5c258eSLen Brown 
3819fcd17211SLen Brown void process_cpuid()
3820103a8feaSLen Brown {
382161a87ba7SLen Brown 	unsigned int eax, ebx, ecx, edx, max_level, max_extended_level;
3822103a8feaSLen Brown 	unsigned int fms, family, model, stepping;
3823b3a34e93SLen Brown 	unsigned int has_turbo;
3824103a8feaSLen Brown 
3825103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
3826103a8feaSLen Brown 
38275aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
3828103a8feaSLen Brown 
3829103a8feaSLen Brown 	if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
3830103a8feaSLen Brown 		genuine_intel = 1;
3831103a8feaSLen Brown 
383296e47158SLen Brown 	if (!quiet)
3833b7d8c148SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s ",
3834103a8feaSLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx);
3835103a8feaSLen Brown 
38365aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
3837103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
3838103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
3839103a8feaSLen Brown 	stepping = fms & 0xf;
3840103a8feaSLen Brown 	if (family == 6 || family == 0xf)
3841103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
3842103a8feaSLen Brown 
384396e47158SLen Brown 	if (!quiet) {
3844b7d8c148SLen Brown 		fprintf(outf, "%d CPUID levels; family:model:stepping 0x%x:%x:%x (%d:%d:%d)\n",
3845103a8feaSLen Brown 			max_level, family, model, stepping, family, model, stepping);
3846aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s\n",
384769807a63SLen Brown 			ecx & (1 << 0) ? "SSE3" : "-",
384869807a63SLen Brown 			ecx & (1 << 3) ? "MONITOR" : "-",
3849aa8d8cc7SLen Brown 			ecx & (1 << 6) ? "SMX" : "-",
385069807a63SLen Brown 			ecx & (1 << 7) ? "EIST" : "-",
385169807a63SLen Brown 			ecx & (1 << 8) ? "TM2" : "-",
385269807a63SLen Brown 			edx & (1 << 4) ? "TSC" : "-",
385369807a63SLen Brown 			edx & (1 << 5) ? "MSR" : "-",
385469807a63SLen Brown 			edx & (1 << 22) ? "ACPI-TM" : "-",
385569807a63SLen Brown 			edx & (1 << 29) ? "TM" : "-");
385669807a63SLen Brown 	}
3857103a8feaSLen Brown 
3858b2c95d90SJosh Triplett 	if (!(edx & (1 << 5)))
3859b2c95d90SJosh Triplett 		errx(1, "CPUID: no MSR");
3860103a8feaSLen Brown 
3861103a8feaSLen Brown 	/*
3862103a8feaSLen Brown 	 * check max extended function levels of CPUID.
3863103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
3864103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
3865103a8feaSLen Brown 	 */
3866103a8feaSLen Brown 	ebx = ecx = edx = 0;
38675aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
3868103a8feaSLen Brown 
386961a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
3870103a8feaSLen Brown 
3871103a8feaSLen Brown 		/*
3872103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
3873103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
3874103a8feaSLen Brown 		 */
38755aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
38768209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
3877d7899447SLen Brown 	}
3878103a8feaSLen Brown 
3879103a8feaSLen Brown 	/*
3880103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
3881103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
3882103a8feaSLen Brown 	 */
3883103a8feaSLen Brown 
38845aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
38858209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
3886812db3f7SLen Brown 	if (has_aperf) {
3887812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
3888812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
3889812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
3890812db3f7SLen Brown 	}
3891889facbeSLen Brown 	do_dts = eax & (1 << 0);
3892812db3f7SLen Brown 	if (do_dts)
3893812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
3894b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
3895889facbeSLen Brown 	do_ptm = eax & (1 << 6);
3896812db3f7SLen Brown 	if (do_ptm)
3897812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
38987f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
38997f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
39007f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
39017f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
39027f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
3903889facbeSLen Brown 	has_epb = ecx & (1 << 3);
3904889facbeSLen Brown 
390596e47158SLen Brown 	if (!quiet)
3906b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
39077f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
39087f5c258eSLen Brown 			has_aperf ? "" : "No-",
3909b3a34e93SLen Brown 			has_turbo ? "" : "No-",
39107f5c258eSLen Brown 			do_dts ? "" : "No-",
39117f5c258eSLen Brown 			do_ptm ? "" : "No-",
39127f5c258eSLen Brown 			has_hwp ? "" : "No-",
39137f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
39147f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
39157f5c258eSLen Brown 			has_hwp_epp ? "" : "No-",
39167f5c258eSLen Brown 			has_hwp_pkg ? "" : "No-",
39177f5c258eSLen Brown 			has_epb ? "" : "No-");
3918103a8feaSLen Brown 
391996e47158SLen Brown 	if (!quiet)
392069807a63SLen Brown 		decode_misc_enable_msr();
392169807a63SLen Brown 
392233148d67SLen Brown 
392396e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
3924aa8d8cc7SLen Brown 		int has_sgx;
3925aa8d8cc7SLen Brown 
3926aa8d8cc7SLen Brown 		ecx = 0;
3927aa8d8cc7SLen Brown 
3928aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
3929aa8d8cc7SLen Brown 
3930aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
3931aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
3932aa8d8cc7SLen Brown 
3933aa8d8cc7SLen Brown 		if (has_sgx)
3934aa8d8cc7SLen Brown 			decode_feature_control_msr();
3935aa8d8cc7SLen Brown 	}
3936aa8d8cc7SLen Brown 
393761a87ba7SLen Brown 	if (max_level >= 0x15) {
39388a5bdf41SLen Brown 		unsigned int eax_crystal;
39398a5bdf41SLen Brown 		unsigned int ebx_tsc;
39408a5bdf41SLen Brown 
39418a5bdf41SLen Brown 		/*
39428a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
39438a5bdf41SLen Brown 		 */
39448a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
39455aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
39468a5bdf41SLen Brown 
39478a5bdf41SLen Brown 		if (ebx_tsc != 0) {
39488a5bdf41SLen Brown 
394996e47158SLen Brown 			if (!quiet && (ebx != 0))
3950b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
39518a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
39528a5bdf41SLen Brown 
39538a5bdf41SLen Brown 			if (crystal_hz == 0)
39548a5bdf41SLen Brown 				switch(model) {
3955869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_MOBILE:	/* SKL */
3956869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_DESKTOP:	/* SKL */
3957869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_MOBILE:	/* KBL */
3958869ce69eSLen Brown 				case INTEL_FAM6_KABYLAKE_DESKTOP:	/* KBL */
3959e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
3960e8efbc80SLen Brown 					break;
3961869ce69eSLen Brown 				case INTEL_FAM6_SKYLAKE_X:	/* SKX */
39627268d407SLen Brown 				case INTEL_FAM6_ATOM_DENVERTON:	/* DNV */
3963ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
3964ec53e594SLen Brown 					break;
3965869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3966ac01ac13SLen Brown 				case INTEL_FAM6_ATOM_GEMINI_LAKE:
3967e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
39688a5bdf41SLen Brown 					break;
39698a5bdf41SLen Brown 				default:
39708a5bdf41SLen Brown 					crystal_hz = 0;
39718a5bdf41SLen Brown 			}
39728a5bdf41SLen Brown 
39738a5bdf41SLen Brown 			if (crystal_hz) {
39748a5bdf41SLen Brown 				tsc_hz =  (unsigned long long) crystal_hz * ebx_tsc / eax_crystal;
397596e47158SLen Brown 				if (!quiet)
3976b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
39778a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc,  eax_crystal);
39788a5bdf41SLen Brown 			}
39798a5bdf41SLen Brown 		}
39808a5bdf41SLen Brown 	}
398161a87ba7SLen Brown 	if (max_level >= 0x16) {
398261a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
398361a87ba7SLen Brown 
398461a87ba7SLen Brown 		/*
398561a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
398661a87ba7SLen Brown 		 */
398761a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
398861a87ba7SLen Brown 
39895aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
399096e47158SLen Brown 		if (!quiet)
3991b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
399261a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
399361a87ba7SLen Brown 	}
39948a5bdf41SLen Brown 
3995b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
3996b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
3997b2b34dfeSHubert Chrzaniuk 
3998812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
3999812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
4000812db3f7SLen Brown 
4001812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
4002812db3f7SLen Brown 		do_nhm_platform_info = 1;
4003812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
4004812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
4005812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
4006812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
4007812db3f7SLen Brown 	}
4008d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
4009812db3f7SLen Brown 
4010812db3f7SLen Brown 	if (do_snb_cstates)
4011812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
4012812db3f7SLen Brown 
40135a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
40140f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
40150f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
40160f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
40170f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
40180f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
40190f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
40200f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
40210f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
40220539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
40230f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
40240f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
40250f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
40260f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
40270539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
40280539ba11SLen Brown 		use_c1_residency_msr = 1;
40290539ba11SLen Brown 	}
40307170a374SLen Brown 	if (is_dnv(family, model)) {
40317170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
40327170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
40337170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
40347170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
40357170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
40367170a374SLen Brown 		use_c1_residency_msr = 1;
40377170a374SLen Brown 	}
403834c76197SLen Brown 	if (is_skx(family, model)) {
403934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
404034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
404134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
404234c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
404334c76197SLen Brown 	}
4044ade0ebacSLen Brown 	if (is_bdx(family, model)) {
4045ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
4046ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
4047ade0ebacSLen Brown 	}
40480f47c08dSLen Brown 	if (has_hsw_msrs(family, model)) {
40490f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc8);
40500f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc9);
40510f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc10);
40520f47c08dSLen Brown 	}
40535a63426eSLen Brown 	do_irtl_hsw = has_hsw_msrs(family, model);
40540b2bb692SLen Brown 	do_skl_residency = has_skl_msrs(family, model);
4055144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
4056fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates  = is_knl(family, model);
4057103a8feaSLen Brown 
405896e47158SLen Brown 	if (!quiet)
4059f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
4060f0057310SLen Brown 
406196e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
406271616c8eSLen Brown 		decode_c6_demotion_policy_msr();
406371616c8eSLen Brown 
4064889facbeSLen Brown 	rapl_probe(family, model);
40653a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
4066889facbeSLen Brown 
406796e47158SLen Brown 	if (!quiet)
40681b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
4069fcd17211SLen Brown 
4070*41618e63SLen Brown 	if (!quiet)
4071*41618e63SLen Brown 		dump_sysfs_cstate_config();
4072*41618e63SLen Brown 
4073a2b7b749SLen Brown 	if (has_skl_msrs(family, model))
4074a2b7b749SLen Brown 		calculate_tsc_tweak();
4075a2b7b749SLen Brown 
4076812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
4077812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
4078fdf676e5SLen Brown 
4079812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
4080812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
408127d47356SLen Brown 
408296e47158SLen Brown 	if (!quiet)
408333148d67SLen Brown 		decode_misc_feature_control();
408433148d67SLen Brown 
4085889facbeSLen Brown 	return;
4086103a8feaSLen Brown }
4087103a8feaSLen Brown 
4088d8af6f5fSLen Brown void help()
4089103a8feaSLen Brown {
4090b7d8c148SLen Brown 	fprintf(outf,
4091d8af6f5fSLen Brown 	"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
4092d8af6f5fSLen Brown 	"\n"
4093d8af6f5fSLen Brown 	"Turbostat forks the specified COMMAND and prints statistics\n"
4094d8af6f5fSLen Brown 	"when COMMAND completes.\n"
4095d8af6f5fSLen Brown 	"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
4096d8af6f5fSLen Brown 	"to print statistics, until interrupted.\n"
4097388e9c81SLen Brown 	"--add		add a counter\n"
4098388e9c81SLen Brown 	"		eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
409996e47158SLen Brown 	"--quiet	skip decoding system configuration header\n"
4100d8af6f5fSLen Brown 	"--interval sec	Override default 5-second measurement interval\n"
4101d8af6f5fSLen Brown 	"--help		print this help message\n"
4102b7d8c148SLen Brown 	"--out file	create or truncate \"file\" for all output\n"
4103d8af6f5fSLen Brown 	"--version	print version information\n"
4104d8af6f5fSLen Brown 	"\n"
4105d8af6f5fSLen Brown 	"For more help, run \"man turbostat\"\n");
4106103a8feaSLen Brown }
4107103a8feaSLen Brown 
4108103a8feaSLen Brown 
4109103a8feaSLen Brown /*
4110103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
4111103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
4112103a8feaSLen Brown  */
4113103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
4114103a8feaSLen Brown {
4115103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
4116103a8feaSLen Brown 		return 1;
4117103a8feaSLen Brown 	else
4118103a8feaSLen Brown 		return 0;
4119103a8feaSLen Brown }
4120103a8feaSLen Brown 
4121103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
4122103a8feaSLen Brown {
4123103a8feaSLen Brown 	return 0;
4124103a8feaSLen Brown }
4125103a8feaSLen Brown 
4126c98d5d94SLen Brown void topology_probe()
4127c98d5d94SLen Brown {
4128c98d5d94SLen Brown 	int i;
4129c98d5d94SLen Brown 	int max_core_id = 0;
4130c98d5d94SLen Brown 	int max_package_id = 0;
4131c98d5d94SLen Brown 	int max_siblings = 0;
4132c98d5d94SLen Brown 	struct cpu_topology {
4133c98d5d94SLen Brown 		int core_id;
4134c98d5d94SLen Brown 		int physical_package_id;
4135c98d5d94SLen Brown 	} *cpus;
4136c98d5d94SLen Brown 
4137c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
4138c98d5d94SLen Brown 	topo.num_cpus = 0;
4139c98d5d94SLen Brown 	topo.max_cpu_num = 0;
4140c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
4141c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
4142812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
4143c98d5d94SLen Brown 
4144d8af6f5fSLen Brown 	if (debug > 1)
4145b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
4146c98d5d94SLen Brown 
4147c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num  + 1) * sizeof(struct cpu_topology));
4148b2c95d90SJosh Triplett 	if (cpus == NULL)
4149b2c95d90SJosh Triplett 		err(1, "calloc cpus");
4150c98d5d94SLen Brown 
4151c98d5d94SLen Brown 	/*
4152c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
4153c98d5d94SLen Brown 	 */
4154c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
4155b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
4156b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4157c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4158c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
4159c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
4160c98d5d94SLen Brown 
4161c98d5d94SLen Brown 	/*
4162c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
4163c98d5d94SLen Brown 	 */
4164c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
4165b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
4166b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
4167c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
4168c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
4169c98d5d94SLen Brown 
4170c98d5d94SLen Brown 
4171c98d5d94SLen Brown 	/*
4172c98d5d94SLen Brown 	 * For online cpus
4173c98d5d94SLen Brown 	 * find max_core_id, max_package_id
4174c98d5d94SLen Brown 	 */
4175c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
4176c98d5d94SLen Brown 		int siblings;
4177c98d5d94SLen Brown 
4178c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
4179d8af6f5fSLen Brown 			if (debug > 1)
4180b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
4181c98d5d94SLen Brown 			continue;
4182c98d5d94SLen Brown 		}
4183c98d5d94SLen Brown 		cpus[i].core_id = get_core_id(i);
4184c98d5d94SLen Brown 		if (cpus[i].core_id > max_core_id)
4185c98d5d94SLen Brown 			max_core_id = cpus[i].core_id;
4186c98d5d94SLen Brown 
4187c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
4188c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
4189c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
4190c98d5d94SLen Brown 
4191c98d5d94SLen Brown 		siblings = get_num_ht_siblings(i);
4192c98d5d94SLen Brown 		if (siblings > max_siblings)
4193c98d5d94SLen Brown 			max_siblings = siblings;
4194d8af6f5fSLen Brown 		if (debug > 1)
4195b7d8c148SLen Brown 			fprintf(outf, "cpu %d pkg %d core %d\n",
4196c98d5d94SLen Brown 				i, cpus[i].physical_package_id, cpus[i].core_id);
4197c98d5d94SLen Brown 	}
4198c98d5d94SLen Brown 	topo.num_cores_per_pkg = max_core_id + 1;
4199d8af6f5fSLen Brown 	if (debug > 1)
4200b7d8c148SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n",
4201c98d5d94SLen Brown 			max_core_id, topo.num_cores_per_pkg);
42020f47c08dSLen Brown 	if (!summary_only && topo.num_cores_per_pkg > 1)
4203812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
4204c98d5d94SLen Brown 
4205c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
4206d8af6f5fSLen Brown 	if (debug > 1)
4207b7d8c148SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n",
4208c98d5d94SLen Brown 			max_package_id, topo.num_packages);
42091cc21f7bSLen Brown 	if (debug && !summary_only && topo.num_packages > 1)
4210812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
4211c98d5d94SLen Brown 
4212c98d5d94SLen Brown 	topo.num_threads_per_core = max_siblings;
4213d8af6f5fSLen Brown 	if (debug > 1)
4214b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
4215c98d5d94SLen Brown 
4216c98d5d94SLen Brown 	free(cpus);
4217c98d5d94SLen Brown }
4218c98d5d94SLen Brown 
4219c98d5d94SLen Brown void
4220c98d5d94SLen Brown allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
4221c98d5d94SLen Brown {
4222c98d5d94SLen Brown 	int i;
4223c98d5d94SLen Brown 
4224c98d5d94SLen Brown 	*t = calloc(topo.num_threads_per_core * topo.num_cores_per_pkg *
4225678a3bd1SLen Brown 		topo.num_packages, sizeof(struct thread_data));
4226c98d5d94SLen Brown 	if (*t == NULL)
4227c98d5d94SLen Brown 		goto error;
4228c98d5d94SLen Brown 
4229c98d5d94SLen Brown 	for (i = 0; i < topo.num_threads_per_core *
4230c98d5d94SLen Brown 		topo.num_cores_per_pkg * topo.num_packages; i++)
4231c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
4232c98d5d94SLen Brown 
4233c98d5d94SLen Brown 	*c = calloc(topo.num_cores_per_pkg * topo.num_packages,
4234678a3bd1SLen Brown 		sizeof(struct core_data));
4235c98d5d94SLen Brown 	if (*c == NULL)
4236c98d5d94SLen Brown 		goto error;
4237c98d5d94SLen Brown 
4238c98d5d94SLen Brown 	for (i = 0; i < topo.num_cores_per_pkg * topo.num_packages; i++)
4239c98d5d94SLen Brown 		(*c)[i].core_id = -1;
4240c98d5d94SLen Brown 
4241678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
4242c98d5d94SLen Brown 	if (*p == NULL)
4243c98d5d94SLen Brown 		goto error;
4244c98d5d94SLen Brown 
4245c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
4246c98d5d94SLen Brown 		(*p)[i].package_id = i;
4247c98d5d94SLen Brown 
4248c98d5d94SLen Brown 	return;
4249c98d5d94SLen Brown error:
4250b2c95d90SJosh Triplett 	err(1, "calloc counters");
4251c98d5d94SLen Brown }
4252c98d5d94SLen Brown /*
4253c98d5d94SLen Brown  * init_counter()
4254c98d5d94SLen Brown  *
4255c98d5d94SLen Brown  * set cpu_id, core_num, pkg_num
4256c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
4257c98d5d94SLen Brown  *
4258c98d5d94SLen Brown  * increment topo.num_cores when 1st core in pkg seen
4259c98d5d94SLen Brown  */
4260c98d5d94SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base,
4261c98d5d94SLen Brown 	struct pkg_data *pkg_base, int thread_num, int core_num,
4262c98d5d94SLen Brown 	int pkg_num, int cpu_id)
4263c98d5d94SLen Brown {
4264c98d5d94SLen Brown 	struct thread_data *t;
4265c98d5d94SLen Brown 	struct core_data *c;
4266c98d5d94SLen Brown 	struct pkg_data *p;
4267c98d5d94SLen Brown 
4268c98d5d94SLen Brown 	t = GET_THREAD(thread_base, thread_num, core_num, pkg_num);
4269c98d5d94SLen Brown 	c = GET_CORE(core_base, core_num, pkg_num);
4270c98d5d94SLen Brown 	p = GET_PKG(pkg_base, pkg_num);
4271c98d5d94SLen Brown 
4272c98d5d94SLen Brown 	t->cpu_id = cpu_id;
4273c98d5d94SLen Brown 	if (thread_num == 0) {
4274c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
4275c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
4276c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
4277c98d5d94SLen Brown 	}
4278c98d5d94SLen Brown 
4279c98d5d94SLen Brown 	c->core_id = core_num;
4280c98d5d94SLen Brown 	p->package_id = pkg_num;
4281c98d5d94SLen Brown }
4282c98d5d94SLen Brown 
4283c98d5d94SLen Brown 
4284c98d5d94SLen Brown int initialize_counters(int cpu_id)
4285c98d5d94SLen Brown {
4286c98d5d94SLen Brown 	int my_thread_id, my_core_id, my_package_id;
4287c98d5d94SLen Brown 
4288c98d5d94SLen Brown 	my_package_id = get_physical_package_id(cpu_id);
4289c98d5d94SLen Brown 	my_core_id = get_core_id(cpu_id);
4290e275b388SDasaratharaman Chandramouli 	my_thread_id = get_cpu_position_in_core(cpu_id);
4291e275b388SDasaratharaman Chandramouli 	if (!my_thread_id)
4292c98d5d94SLen Brown 		topo.num_cores++;
4293c98d5d94SLen Brown 
4294c98d5d94SLen Brown 	init_counter(EVEN_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4295c98d5d94SLen Brown 	init_counter(ODD_COUNTERS, my_thread_id, my_core_id, my_package_id, cpu_id);
4296c98d5d94SLen Brown 	return 0;
4297c98d5d94SLen Brown }
4298c98d5d94SLen Brown 
4299c98d5d94SLen Brown void allocate_output_buffer()
4300c98d5d94SLen Brown {
43013b4d5c7fSAndy Shevchenko 	output_buffer = calloc(1, (1 + topo.num_cpus) * 1024);
4302c98d5d94SLen Brown 	outp = output_buffer;
4303b2c95d90SJosh Triplett 	if (outp == NULL)
4304b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
4305c98d5d94SLen Brown }
430636229897SLen Brown void allocate_fd_percpu(void)
430736229897SLen Brown {
430801a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
430936229897SLen Brown 	if (fd_percpu == NULL)
431036229897SLen Brown 		err(-1, "calloc fd_percpu");
431136229897SLen Brown }
4312562a2d37SLen Brown void allocate_irq_buffers(void)
4313562a2d37SLen Brown {
4314562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
4315562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
4316562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
4317c98d5d94SLen Brown 
431801a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
4319562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
432001a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
4321562a2d37SLen Brown }
4322c98d5d94SLen Brown void setup_all_buffers(void)
4323c98d5d94SLen Brown {
4324c98d5d94SLen Brown 	topology_probe();
4325562a2d37SLen Brown 	allocate_irq_buffers();
432636229897SLen Brown 	allocate_fd_percpu();
4327c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
4328c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
4329c98d5d94SLen Brown 	allocate_output_buffer();
4330c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
4331c98d5d94SLen Brown }
43323b4d5c7fSAndy Shevchenko 
43337ce7d5deSPrarit Bhargava void set_base_cpu(void)
43347ce7d5deSPrarit Bhargava {
43357ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
43367ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
43377ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
43387ce7d5deSPrarit Bhargava 
43397ce7d5deSPrarit Bhargava 	if (debug > 1)
4340b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
43417ce7d5deSPrarit Bhargava }
43427ce7d5deSPrarit Bhargava 
4343103a8feaSLen Brown void turbostat_init()
4344103a8feaSLen Brown {
43457ce7d5deSPrarit Bhargava 	setup_all_buffers();
43467ce7d5deSPrarit Bhargava 	set_base_cpu();
4347103a8feaSLen Brown 	check_dev_msr();
434898481e79SLen Brown 	check_permissions();
4349fcd17211SLen Brown 	process_cpuid();
4350103a8feaSLen Brown 
4351103a8feaSLen Brown 
435296e47158SLen Brown 	if (!quiet)
43537f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
43547f5c258eSLen Brown 
435596e47158SLen Brown 	if (!quiet)
4356889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
4357889facbeSLen Brown 
435896e47158SLen Brown 	if (!quiet)
43593a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
43603a9a941dSLen Brown 
436196e47158SLen Brown 	if (!quiet)
4362889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
4363889facbeSLen Brown 
4364889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
4365889facbeSLen Brown 
436696e47158SLen Brown 	if (!quiet)
4367889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
43685a63426eSLen Brown 
436996e47158SLen Brown 	if (!quiet && do_irtl_snb)
43705a63426eSLen Brown 		print_irtl();
4371103a8feaSLen Brown }
4372103a8feaSLen Brown 
4373103a8feaSLen Brown int fork_it(char **argv)
4374103a8feaSLen Brown {
4375103a8feaSLen Brown 	pid_t child_pid;
4376d91bb17cSLen Brown 	int status;
4377d15cf7c1SLen Brown 
4378d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
4379d91bb17cSLen Brown 	if (status)
4380d91bb17cSLen Brown 		exit(status);
4381d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
4382d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
4383103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
4384103a8feaSLen Brown 
4385103a8feaSLen Brown 	child_pid = fork();
4386103a8feaSLen Brown 	if (!child_pid) {
4387103a8feaSLen Brown 		/* child */
4388103a8feaSLen Brown 		execvp(argv[0], argv);
4389103a8feaSLen Brown 	} else {
4390103a8feaSLen Brown 
4391103a8feaSLen Brown 		/* parent */
4392b2c95d90SJosh Triplett 		if (child_pid == -1)
4393b2c95d90SJosh Triplett 			err(1, "fork");
4394103a8feaSLen Brown 
4395103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
4396103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
4397b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
4398b2c95d90SJosh Triplett 			err(status, "waitpid");
4399103a8feaSLen Brown 	}
4400c98d5d94SLen Brown 	/*
4401c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
4402c98d5d94SLen Brown 	 * because re-starting is problematic when forking
4403c98d5d94SLen Brown 	 */
4404c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
4405103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
4406103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
4407ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
4408ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
4409ba3dec99SLen Brown 	else {
4410c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
4411c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
4412ba3dec99SLen Brown 	}
4413103a8feaSLen Brown 
4414b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec/1000000.0);
4415b7d8c148SLen Brown 
4416b7d8c148SLen Brown 	flush_output_stderr();
4417103a8feaSLen Brown 
4418d91bb17cSLen Brown 	return status;
4419103a8feaSLen Brown }
4420103a8feaSLen Brown 
44213b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
44223b4d5c7fSAndy Shevchenko {
44233b4d5c7fSAndy Shevchenko 	int status;
44243b4d5c7fSAndy Shevchenko 
44253b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
44263b4d5c7fSAndy Shevchenko 	if (status)
44273b4d5c7fSAndy Shevchenko 		return status;
44283b4d5c7fSAndy Shevchenko 
44293b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
44303b4d5c7fSAndy Shevchenko 	if (status)
44313b4d5c7fSAndy Shevchenko 		return status;
44323b4d5c7fSAndy Shevchenko 
4433b7d8c148SLen Brown 	flush_output_stdout();
44343b4d5c7fSAndy Shevchenko 
44353b4d5c7fSAndy Shevchenko 	return status;
44363b4d5c7fSAndy Shevchenko }
44373b4d5c7fSAndy Shevchenko 
4438d8af6f5fSLen Brown void print_version() {
44390539ba11SLen Brown 	fprintf(outf, "turbostat version 4.17 10 Jan 2017"
4440d8af6f5fSLen Brown 		" - Len Brown <lenb@kernel.org>\n");
4441d8af6f5fSLen Brown }
4442d8af6f5fSLen Brown 
4443495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
4444495c7654SLen Brown 	unsigned int width, enum counter_scope scope,
4445*41618e63SLen Brown 	enum counter_type type, enum counter_format format, int flags)
4446388e9c81SLen Brown {
4447388e9c81SLen Brown 	struct msr_counter *msrp;
4448388e9c81SLen Brown 
4449388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
4450388e9c81SLen Brown 	if (msrp == NULL) {
4451388e9c81SLen Brown 		perror("calloc");
4452388e9c81SLen Brown 		exit(1);
4453388e9c81SLen Brown 	}
4454388e9c81SLen Brown 
4455388e9c81SLen Brown 	msrp->msr_num = msr_num;
4456388e9c81SLen Brown 	strncpy(msrp->name, name, NAME_BYTES);
4457495c7654SLen Brown 	if (path)
4458495c7654SLen Brown 		strncpy(msrp->path, path, PATH_BYTES);
4459388e9c81SLen Brown 	msrp->width = width;
4460388e9c81SLen Brown 	msrp->type = type;
4461388e9c81SLen Brown 	msrp->format = format;
4462*41618e63SLen Brown 	msrp->flags = flags;
4463388e9c81SLen Brown 
4464388e9c81SLen Brown 	switch (scope) {
4465388e9c81SLen Brown 
4466388e9c81SLen Brown 	case SCOPE_CPU:
4467388e9c81SLen Brown 		msrp->next = sys.tp;
4468388e9c81SLen Brown 		sys.tp = msrp;
4469678a3bd1SLen Brown 		sys.added_thread_counters++;
4470678a3bd1SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_COUNTERS) {
4471678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n",
4472678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4473678a3bd1SLen Brown 			exit(-1);
4474678a3bd1SLen Brown 		}
4475388e9c81SLen Brown 		break;
4476388e9c81SLen Brown 
4477388e9c81SLen Brown 	case SCOPE_CORE:
4478388e9c81SLen Brown 		msrp->next = sys.cp;
4479388e9c81SLen Brown 		sys.cp = msrp;
4480678a3bd1SLen Brown 		sys.added_core_counters++;
4481678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
4482678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n",
4483678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4484678a3bd1SLen Brown 			exit(-1);
4485678a3bd1SLen Brown 		}
4486388e9c81SLen Brown 		break;
4487388e9c81SLen Brown 
4488388e9c81SLen Brown 	case SCOPE_PACKAGE:
4489388e9c81SLen Brown 		msrp->next = sys.pp;
4490388e9c81SLen Brown 		sys.pp = msrp;
4491678a3bd1SLen Brown 		sys.added_package_counters++;
4492678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
4493678a3bd1SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n",
4494678a3bd1SLen Brown 				MAX_ADDED_COUNTERS);
4495678a3bd1SLen Brown 			exit(-1);
4496678a3bd1SLen Brown 		}
4497388e9c81SLen Brown 		break;
4498388e9c81SLen Brown 	}
4499388e9c81SLen Brown 
4500388e9c81SLen Brown 	return 0;
4501388e9c81SLen Brown }
4502388e9c81SLen Brown 
4503388e9c81SLen Brown void parse_add_command(char *add_command)
4504388e9c81SLen Brown {
4505388e9c81SLen Brown 	int msr_num = 0;
4506495c7654SLen Brown 	char *path = NULL;
45070f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
4508388e9c81SLen Brown 	int width = 64;
4509388e9c81SLen Brown 	int fail = 0;
4510388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
4511388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
4512388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
4513388e9c81SLen Brown 
4514388e9c81SLen Brown 	while (add_command) {
4515388e9c81SLen Brown 
4516388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
4517388e9c81SLen Brown 			goto next;
4518388e9c81SLen Brown 
4519388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
4520388e9c81SLen Brown 			goto next;
4521388e9c81SLen Brown 
4522495c7654SLen Brown 		if (*add_command == '/') {
4523495c7654SLen Brown 			path = add_command;
4524495c7654SLen Brown 			goto next;
4525495c7654SLen Brown 		}
4526495c7654SLen Brown 
4527388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
4528388e9c81SLen Brown 			if ((width == 32) || (width == 64))
4529388e9c81SLen Brown 				goto next;
4530388e9c81SLen Brown 			width = 64;
4531388e9c81SLen Brown 		}
4532388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
4533388e9c81SLen Brown 			scope = SCOPE_CPU;
4534388e9c81SLen Brown 			goto next;
4535388e9c81SLen Brown 		}
4536388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
4537388e9c81SLen Brown 			scope = SCOPE_CORE;
4538388e9c81SLen Brown 			goto next;
4539388e9c81SLen Brown 		}
4540388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
4541388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
4542388e9c81SLen Brown 			goto next;
4543388e9c81SLen Brown 		}
4544388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
4545388e9c81SLen Brown 			type = COUNTER_CYCLES;
4546388e9c81SLen Brown 			goto next;
4547388e9c81SLen Brown 		}
4548388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
4549388e9c81SLen Brown 			type = COUNTER_SECONDS;
4550388e9c81SLen Brown 			goto next;
4551388e9c81SLen Brown 		}
4552*41618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
4553*41618e63SLen Brown 			type = COUNTER_USEC;
4554*41618e63SLen Brown 			goto next;
4555*41618e63SLen Brown 		}
4556388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
4557388e9c81SLen Brown 			format = FORMAT_RAW;
4558388e9c81SLen Brown 			goto next;
4559388e9c81SLen Brown 		}
4560388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
4561388e9c81SLen Brown 			format = FORMAT_DELTA;
4562388e9c81SLen Brown 			goto next;
4563388e9c81SLen Brown 		}
4564388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
4565388e9c81SLen Brown 			format = FORMAT_PERCENT;
4566388e9c81SLen Brown 			goto next;
4567388e9c81SLen Brown 		}
4568388e9c81SLen Brown 
4569388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
4570388e9c81SLen Brown 			char *eos;
4571388e9c81SLen Brown 
4572388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
4573388e9c81SLen Brown 			if (eos)
4574388e9c81SLen Brown 				*eos = '\0';
4575388e9c81SLen Brown 			goto next;
4576388e9c81SLen Brown 		}
4577388e9c81SLen Brown 
4578388e9c81SLen Brown next:
4579388e9c81SLen Brown 		add_command = strchr(add_command, ',');
4580495c7654SLen Brown 		if (add_command) {
4581495c7654SLen Brown 			*add_command = '\0';
4582388e9c81SLen Brown 			add_command++;
4583495c7654SLen Brown 		}
4584388e9c81SLen Brown 
4585388e9c81SLen Brown 	}
4586495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
4587495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
4588388e9c81SLen Brown 		fail++;
4589388e9c81SLen Brown 	}
4590388e9c81SLen Brown 
4591388e9c81SLen Brown 	/* generate default column header */
4592388e9c81SLen Brown 	if (*name_buffer == '\0') {
4593388e9c81SLen Brown 		if (format == FORMAT_RAW) {
4594388e9c81SLen Brown 			if (width == 32)
4595388e9c81SLen Brown 				sprintf(name_buffer, "msr%d", msr_num);
4596388e9c81SLen Brown 			else
4597388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d", msr_num);
4598388e9c81SLen Brown 		} else if (format == FORMAT_DELTA) {
4599388e9c81SLen Brown 			if (width == 32)
4600388e9c81SLen Brown 				sprintf(name_buffer, "cnt%d", msr_num);
4601388e9c81SLen Brown 			else
4602388e9c81SLen Brown 				sprintf(name_buffer, "CNT%d", msr_num);
4603388e9c81SLen Brown 		} else if (format == FORMAT_PERCENT) {
4604388e9c81SLen Brown 			if (width == 32)
4605388e9c81SLen Brown 				sprintf(name_buffer, "msr%d%%", msr_num);
4606388e9c81SLen Brown 			else
4607388e9c81SLen Brown 				sprintf(name_buffer, "MSR%d%%", msr_num);
4608388e9c81SLen Brown 		}
4609388e9c81SLen Brown 	}
4610388e9c81SLen Brown 
4611*41618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
4612388e9c81SLen Brown 		fail++;
4613388e9c81SLen Brown 
4614388e9c81SLen Brown 	if (fail) {
4615388e9c81SLen Brown 		help();
4616388e9c81SLen Brown 		exit(1);
4617388e9c81SLen Brown 	}
4618388e9c81SLen Brown }
4619*41618e63SLen Brown 
4620*41618e63SLen Brown void probe_sysfs(void)
4621*41618e63SLen Brown {
4622*41618e63SLen Brown 	char path[64];
4623*41618e63SLen Brown 	char name_buf[16];
4624*41618e63SLen Brown 	FILE *input;
4625*41618e63SLen Brown 	int state;
4626*41618e63SLen Brown 	char *sp;
4627*41618e63SLen Brown 
4628*41618e63SLen Brown 	if (!DO_BIC(BIC_sysfs))
4629*41618e63SLen Brown 		return;
4630*41618e63SLen Brown 
4631*41618e63SLen Brown 	for (state = 10; state > 0; --state) {
4632*41618e63SLen Brown 
4633*41618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4634*41618e63SLen Brown 			base_cpu, state);
4635*41618e63SLen Brown 		input = fopen(path, "r");
4636*41618e63SLen Brown 		if (input == NULL)
4637*41618e63SLen Brown 			continue;
4638*41618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
4639*41618e63SLen Brown 
4640*41618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4641*41618e63SLen Brown 		sp = strchr(name_buf, '-');
4642*41618e63SLen Brown 		if (!sp)
4643*41618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
4644*41618e63SLen Brown 		*sp = '%';
4645*41618e63SLen Brown 		*(sp + 1) = '\0';
4646*41618e63SLen Brown 
4647*41618e63SLen Brown 		fclose(input);
4648*41618e63SLen Brown 
4649*41618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
4650*41618e63SLen Brown 
4651*41618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC,
4652*41618e63SLen Brown 				FORMAT_PERCENT, SYSFS_PERCPU);
4653*41618e63SLen Brown 	}
4654*41618e63SLen Brown 
4655*41618e63SLen Brown 	for (state = 10; state > 0; --state) {
4656*41618e63SLen Brown 
4657*41618e63SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name",
4658*41618e63SLen Brown 			base_cpu, state);
4659*41618e63SLen Brown 		input = fopen(path, "r");
4660*41618e63SLen Brown 		if (input == NULL)
4661*41618e63SLen Brown 			continue;
4662*41618e63SLen Brown 		fgets(name_buf, sizeof(name_buf), input);
4663*41618e63SLen Brown 		 /* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
4664*41618e63SLen Brown 		sp = strchr(name_buf, '-');
4665*41618e63SLen Brown 		if (!sp)
4666*41618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
4667*41618e63SLen Brown 		*sp = '\0';
4668*41618e63SLen Brown 		fclose(input);
4669*41618e63SLen Brown 
4670*41618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
4671*41618e63SLen Brown 
4672*41618e63SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS,
4673*41618e63SLen Brown 				FORMAT_DELTA, SYSFS_PERCPU);
4674*41618e63SLen Brown 	}
4675*41618e63SLen Brown 
4676*41618e63SLen Brown }
4677*41618e63SLen Brown 
4678812db3f7SLen Brown /*
4679812db3f7SLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
4680812db3f7SLen Brown  * SHOW_LIST - show this list of counters, hide the rest
4681812db3f7SLen Brown  */
4682812db3f7SLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
4683812db3f7SLen Brown 
4684812db3f7SLen Brown int shown;
4685812db3f7SLen Brown /*
4686812db3f7SLen Brown  * parse_show_hide() - process cmdline to set default counter action
4687812db3f7SLen Brown  */
4688812db3f7SLen Brown void parse_show_hide(char *optarg, enum show_hide_mode new_mode)
4689812db3f7SLen Brown {
4690812db3f7SLen Brown 	/*
4691812db3f7SLen Brown 	 * --show: show only those specified
4692812db3f7SLen Brown 	 *  The 1st invocation will clear and replace the enabled mask
4693812db3f7SLen Brown 	 *  subsequent invocations can add to it.
4694812db3f7SLen Brown 	 */
4695812db3f7SLen Brown 	if (new_mode == SHOW_LIST) {
4696812db3f7SLen Brown 		if (shown == 0)
4697812db3f7SLen Brown 			bic_enabled = bic_lookup(optarg);
4698812db3f7SLen Brown 		else
4699812db3f7SLen Brown 			bic_enabled |= bic_lookup(optarg);
4700812db3f7SLen Brown 		shown = 1;
4701812db3f7SLen Brown 
4702812db3f7SLen Brown 		return;
4703812db3f7SLen Brown 	}
4704812db3f7SLen Brown 
4705812db3f7SLen Brown 	/*
4706812db3f7SLen Brown 	 * --hide: do not show those specified
4707812db3f7SLen Brown 	 *  multiple invocations simply clear more bits in enabled mask
4708812db3f7SLen Brown 	 */
4709812db3f7SLen Brown 	bic_enabled &= ~bic_lookup(optarg);
4710*41618e63SLen Brown 
4711812db3f7SLen Brown }
4712812db3f7SLen Brown 
4713103a8feaSLen Brown void cmdline(int argc, char **argv)
4714103a8feaSLen Brown {
4715103a8feaSLen Brown 	int opt;
4716d8af6f5fSLen Brown 	int option_index = 0;
4717d8af6f5fSLen Brown 	static struct option long_options[] = {
4718388e9c81SLen Brown 		{"add",		required_argument,	0, 'a'},
4719d8af6f5fSLen Brown 		{"Dump",	no_argument,		0, 'D'},
472096e47158SLen Brown 		{"debug",	no_argument,		0, 'd'},	/* internal, not documented */
4721d8af6f5fSLen Brown 		{"interval",	required_argument,	0, 'i'},
4722d8af6f5fSLen Brown 		{"help",	no_argument,		0, 'h'},
4723812db3f7SLen Brown 		{"hide",	required_argument,	0, 'H'},	// meh, -h taken by --help
4724d8af6f5fSLen Brown 		{"Joules",	no_argument,		0, 'J'},
4725b7d8c148SLen Brown 		{"out",		required_argument,	0, 'o'},
4726d8af6f5fSLen Brown 		{"Package",	no_argument,		0, 'p'},
4727d8af6f5fSLen Brown 		{"processor",	no_argument,		0, 'p'},
472896e47158SLen Brown 		{"quiet",	no_argument,		0, 'q'},
4729812db3f7SLen Brown 		{"show",	required_argument,	0, 's'},
4730d8af6f5fSLen Brown 		{"Summary",	no_argument,		0, 'S'},
4731d8af6f5fSLen Brown 		{"TCC",		required_argument,	0, 'T'},
4732d8af6f5fSLen Brown 		{"version",	no_argument,		0, 'v' },
4733d8af6f5fSLen Brown 		{0,		0,			0,  0 }
4734d8af6f5fSLen Brown 	};
4735103a8feaSLen Brown 
4736103a8feaSLen Brown 	progname = argv[0];
4737103a8feaSLen Brown 
473896e47158SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Ddhi:JM:m:o:PpqST:v",
4739d8af6f5fSLen Brown 				long_options, &option_index)) != -1) {
4740103a8feaSLen Brown 		switch (opt) {
4741388e9c81SLen Brown 		case 'a':
4742388e9c81SLen Brown 			parse_add_command(optarg);
4743388e9c81SLen Brown 			break;
4744d8af6f5fSLen Brown 		case 'D':
4745d8af6f5fSLen Brown 			dump_only++;
47468e180f3cSLen Brown 			break;
4747d8af6f5fSLen Brown 		case 'd':
4748d8af6f5fSLen Brown 			debug++;
47492f32edf1SLen Brown 			break;
4750812db3f7SLen Brown 		case 'H':
4751812db3f7SLen Brown 			parse_show_hide(optarg, HIDE_LIST);
4752812db3f7SLen Brown 			break;
4753d8af6f5fSLen Brown 		case 'h':
4754d8af6f5fSLen Brown 		default:
4755d8af6f5fSLen Brown 			help();
4756d8af6f5fSLen Brown 			exit(1);
4757d8af6f5fSLen Brown 		case 'i':
47582a0609c0SLen Brown 			{
47592a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
47602a0609c0SLen Brown 
47612a0609c0SLen Brown 				if (interval < 0.001) {
4762b7d8c148SLen Brown 					fprintf(outf, "interval %f seconds is too small\n",
47632a0609c0SLen Brown 						interval);
47642a0609c0SLen Brown 					exit(2);
47652a0609c0SLen Brown 				}
47662a0609c0SLen Brown 
47672a0609c0SLen Brown 				interval_ts.tv_sec = interval;
47682a0609c0SLen Brown 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
47692a0609c0SLen Brown 			}
4770889facbeSLen Brown 			break;
47715c56be9aSDirk Brandewie 		case 'J':
47725c56be9aSDirk Brandewie 			rapl_joules++;
47735c56be9aSDirk Brandewie 			break;
4774b7d8c148SLen Brown 		case 'o':
4775b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
4776b7d8c148SLen Brown 			break;
4777d8af6f5fSLen Brown 		case 'P':
4778d8af6f5fSLen Brown 			show_pkg_only++;
4779d8af6f5fSLen Brown 			break;
4780d8af6f5fSLen Brown 		case 'p':
4781d8af6f5fSLen Brown 			show_core_only++;
4782d8af6f5fSLen Brown 			break;
478396e47158SLen Brown 		case 'q':
478496e47158SLen Brown 			quiet = 1;
478596e47158SLen Brown 			break;
4786812db3f7SLen Brown 		case 's':
4787812db3f7SLen Brown 			parse_show_hide(optarg, SHOW_LIST);
4788812db3f7SLen Brown 			break;
4789d8af6f5fSLen Brown 		case 'S':
4790d8af6f5fSLen Brown 			summary_only++;
4791d8af6f5fSLen Brown 			break;
4792d8af6f5fSLen Brown 		case 'T':
4793d8af6f5fSLen Brown 			tcc_activation_temp_override = atoi(optarg);
4794d8af6f5fSLen Brown 			break;
4795d8af6f5fSLen Brown 		case 'v':
4796d8af6f5fSLen Brown 			print_version();
4797d8af6f5fSLen Brown 			exit(0);
4798d8af6f5fSLen Brown 			break;
4799103a8feaSLen Brown 		}
4800103a8feaSLen Brown 	}
4801103a8feaSLen Brown }
4802103a8feaSLen Brown 
4803103a8feaSLen Brown int main(int argc, char **argv)
4804103a8feaSLen Brown {
4805b7d8c148SLen Brown 	outf = stderr;
4806b7d8c148SLen Brown 
4807103a8feaSLen Brown 	cmdline(argc, argv);
4808103a8feaSLen Brown 
480996e47158SLen Brown 	if (!quiet)
4810d8af6f5fSLen Brown 		print_version();
4811103a8feaSLen Brown 
4812*41618e63SLen Brown 	probe_sysfs();
4813*41618e63SLen Brown 
4814103a8feaSLen Brown 	turbostat_init();
4815103a8feaSLen Brown 
48163b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
48173b4d5c7fSAndy Shevchenko 	if (dump_only)
48183b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
48193b4d5c7fSAndy Shevchenko 
4820103a8feaSLen Brown 	/*
4821103a8feaSLen Brown 	 * if any params left, it must be a command to fork
4822103a8feaSLen Brown 	 */
4823103a8feaSLen Brown 	if (argc - optind)
4824103a8feaSLen Brown 		return fork_it(argv + optind);
4825103a8feaSLen Brown 	else
4826103a8feaSLen Brown 		turbostat_loop();
4827103a8feaSLen Brown 
4828103a8feaSLen Brown 	return 0;
4829103a8feaSLen Brown }
4830