xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 164d7a965b3e31b1ea109d2bf5dd68e1b41e020f)
1a61127c2SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2103a8feaSLen Brown /*
3103a8feaSLen Brown  * turbostat -- show CPU frequency and C-state residency
434041551SLen Brown  * on modern Intel and AMD processors.
5103a8feaSLen Brown  *
63c070b2aSLen Brown  * Copyright (c) 2021 Intel Corporation.
7103a8feaSLen Brown  * Len Brown <len.brown@intel.com>
8103a8feaSLen Brown  */
9103a8feaSLen Brown 
1088c3281fSLen Brown #define _GNU_SOURCE
11b731f311SJosh Triplett #include MSRHEADER
12869ce69eSLen Brown #include INTEL_FAMILY_HEADER
1395aebc44SJosh Triplett #include <stdarg.h>
14103a8feaSLen Brown #include <stdio.h>
15b2c95d90SJosh Triplett #include <err.h>
16103a8feaSLen Brown #include <unistd.h>
17103a8feaSLen Brown #include <sys/types.h>
18103a8feaSLen Brown #include <sys/wait.h>
19103a8feaSLen Brown #include <sys/stat.h>
20b9ad8ee0SLen Brown #include <sys/select.h>
21103a8feaSLen Brown #include <sys/resource.h>
22103a8feaSLen Brown #include <fcntl.h>
23103a8feaSLen Brown #include <signal.h>
24103a8feaSLen Brown #include <sys/time.h>
25103a8feaSLen Brown #include <stdlib.h>
26d8af6f5fSLen Brown #include <getopt.h>
27103a8feaSLen Brown #include <dirent.h>
28103a8feaSLen Brown #include <string.h>
29103a8feaSLen Brown #include <ctype.h>
3088c3281fSLen Brown #include <sched.h>
312a0609c0SLen Brown #include <time.h>
322b92865eSJosh Triplett #include <cpuid.h>
33fcaa681cSLen Brown #include <sys/capability.h>
3498481e79SLen Brown #include <errno.h>
359392bd98SCalvin Walton #include <math.h>
362af4f9b8SLen Brown #include <linux/perf_event.h>
372af4f9b8SLen Brown #include <asm/unistd.h>
387ab5ff49SZhang Rui #include <stdbool.h>
39103a8feaSLen Brown 
40*164d7a96SLen Brown /*
41*164d7a96SLen Brown  * This list matches the column headers, except
42*164d7a96SLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
43*164d7a96SLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
44*164d7a96SLen Brown  *    matching on them for --show and --hide.
45*164d7a96SLen Brown  */
46*164d7a96SLen Brown 
47*164d7a96SLen Brown /*
48*164d7a96SLen Brown  * buffer size used by sscanf() for added column names
49*164d7a96SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
50*164d7a96SLen Brown  */
51*164d7a96SLen Brown #define	NAME_BYTES 20
52*164d7a96SLen Brown #define PATH_BYTES 128
53*164d7a96SLen Brown 
54*164d7a96SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE };
55*164d7a96SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC };
56*164d7a96SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT };
57*164d7a96SLen Brown 
58*164d7a96SLen Brown struct msr_counter {
59*164d7a96SLen Brown 	unsigned int msr_num;
60*164d7a96SLen Brown 	char name[NAME_BYTES];
61*164d7a96SLen Brown 	char path[PATH_BYTES];
62*164d7a96SLen Brown 	unsigned int width;
63*164d7a96SLen Brown 	enum counter_type type;
64*164d7a96SLen Brown 	enum counter_format format;
65*164d7a96SLen Brown 	struct msr_counter *next;
66*164d7a96SLen Brown 	unsigned int flags;
67*164d7a96SLen Brown #define	FLAGS_HIDE	(1 << 0)
68*164d7a96SLen Brown #define	FLAGS_SHOW	(1 << 1)
69*164d7a96SLen Brown #define	SYSFS_PERCPU	(1 << 1)
70*164d7a96SLen Brown };
71*164d7a96SLen Brown 
72*164d7a96SLen Brown struct msr_counter bic[] = {
73*164d7a96SLen Brown 	{ 0x0, "usec" },
74*164d7a96SLen Brown 	{ 0x0, "Time_Of_Day_Seconds" },
75*164d7a96SLen Brown 	{ 0x0, "Package" },
76*164d7a96SLen Brown 	{ 0x0, "Node" },
77*164d7a96SLen Brown 	{ 0x0, "Avg_MHz" },
78*164d7a96SLen Brown 	{ 0x0, "Busy%" },
79*164d7a96SLen Brown 	{ 0x0, "Bzy_MHz" },
80*164d7a96SLen Brown 	{ 0x0, "TSC_MHz" },
81*164d7a96SLen Brown 	{ 0x0, "IRQ" },
82*164d7a96SLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL },
83*164d7a96SLen Brown 	{ 0x0, "sysfs" },
84*164d7a96SLen Brown 	{ 0x0, "CPU%c1" },
85*164d7a96SLen Brown 	{ 0x0, "CPU%c3" },
86*164d7a96SLen Brown 	{ 0x0, "CPU%c6" },
87*164d7a96SLen Brown 	{ 0x0, "CPU%c7" },
88*164d7a96SLen Brown 	{ 0x0, "ThreadC" },
89*164d7a96SLen Brown 	{ 0x0, "CoreTmp" },
90*164d7a96SLen Brown 	{ 0x0, "CoreCnt" },
91*164d7a96SLen Brown 	{ 0x0, "PkgTmp" },
92*164d7a96SLen Brown 	{ 0x0, "GFX%rc6" },
93*164d7a96SLen Brown 	{ 0x0, "GFXMHz" },
94*164d7a96SLen Brown 	{ 0x0, "Pkg%pc2" },
95*164d7a96SLen Brown 	{ 0x0, "Pkg%pc3" },
96*164d7a96SLen Brown 	{ 0x0, "Pkg%pc6" },
97*164d7a96SLen Brown 	{ 0x0, "Pkg%pc7" },
98*164d7a96SLen Brown 	{ 0x0, "Pkg%pc8" },
99*164d7a96SLen Brown 	{ 0x0, "Pkg%pc9" },
100*164d7a96SLen Brown 	{ 0x0, "Pk%pc10" },
101*164d7a96SLen Brown 	{ 0x0, "CPU%LPI" },
102*164d7a96SLen Brown 	{ 0x0, "SYS%LPI" },
103*164d7a96SLen Brown 	{ 0x0, "PkgWatt" },
104*164d7a96SLen Brown 	{ 0x0, "CorWatt" },
105*164d7a96SLen Brown 	{ 0x0, "GFXWatt" },
106*164d7a96SLen Brown 	{ 0x0, "PkgCnt" },
107*164d7a96SLen Brown 	{ 0x0, "RAMWatt" },
108*164d7a96SLen Brown 	{ 0x0, "PKG_%" },
109*164d7a96SLen Brown 	{ 0x0, "RAM_%" },
110*164d7a96SLen Brown 	{ 0x0, "Pkg_J" },
111*164d7a96SLen Brown 	{ 0x0, "Cor_J" },
112*164d7a96SLen Brown 	{ 0x0, "GFX_J" },
113*164d7a96SLen Brown 	{ 0x0, "RAM_J" },
114*164d7a96SLen Brown 	{ 0x0, "Mod%c6" },
115*164d7a96SLen Brown 	{ 0x0, "Totl%C0" },
116*164d7a96SLen Brown 	{ 0x0, "Any%C0" },
117*164d7a96SLen Brown 	{ 0x0, "GFX%C0" },
118*164d7a96SLen Brown 	{ 0x0, "CPUGFX%" },
119*164d7a96SLen Brown 	{ 0x0, "Core" },
120*164d7a96SLen Brown 	{ 0x0, "CPU" },
121*164d7a96SLen Brown 	{ 0x0, "APIC" },
122*164d7a96SLen Brown 	{ 0x0, "X2APIC" },
123*164d7a96SLen Brown 	{ 0x0, "Die" },
124*164d7a96SLen Brown 	{ 0x0, "GFXAMHz" },
125*164d7a96SLen Brown 	{ 0x0, "IPC" },
126*164d7a96SLen Brown 	{ 0x0, "CoreThr" },
127*164d7a96SLen Brown };
128*164d7a96SLen Brown 
129*164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
130*164d7a96SLen Brown #define	BIC_USEC	(1ULL << 0)
131*164d7a96SLen Brown #define	BIC_TOD		(1ULL << 1)
132*164d7a96SLen Brown #define	BIC_Package	(1ULL << 2)
133*164d7a96SLen Brown #define	BIC_Node	(1ULL << 3)
134*164d7a96SLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
135*164d7a96SLen Brown #define	BIC_Busy	(1ULL << 5)
136*164d7a96SLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
137*164d7a96SLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
138*164d7a96SLen Brown #define	BIC_IRQ		(1ULL << 8)
139*164d7a96SLen Brown #define	BIC_SMI		(1ULL << 9)
140*164d7a96SLen Brown #define	BIC_sysfs	(1ULL << 10)
141*164d7a96SLen Brown #define	BIC_CPU_c1	(1ULL << 11)
142*164d7a96SLen Brown #define	BIC_CPU_c3	(1ULL << 12)
143*164d7a96SLen Brown #define	BIC_CPU_c6	(1ULL << 13)
144*164d7a96SLen Brown #define	BIC_CPU_c7	(1ULL << 14)
145*164d7a96SLen Brown #define	BIC_ThreadC	(1ULL << 15)
146*164d7a96SLen Brown #define	BIC_CoreTmp	(1ULL << 16)
147*164d7a96SLen Brown #define	BIC_CoreCnt	(1ULL << 17)
148*164d7a96SLen Brown #define	BIC_PkgTmp	(1ULL << 18)
149*164d7a96SLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
150*164d7a96SLen Brown #define	BIC_GFXMHz	(1ULL << 20)
151*164d7a96SLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
152*164d7a96SLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
153*164d7a96SLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
154*164d7a96SLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
155*164d7a96SLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
156*164d7a96SLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
157*164d7a96SLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
158*164d7a96SLen Brown #define BIC_CPU_LPI	(1ULL << 28)
159*164d7a96SLen Brown #define BIC_SYS_LPI	(1ULL << 29)
160*164d7a96SLen Brown #define	BIC_PkgWatt	(1ULL << 30)
161*164d7a96SLen Brown #define	BIC_CorWatt	(1ULL << 31)
162*164d7a96SLen Brown #define	BIC_GFXWatt	(1ULL << 32)
163*164d7a96SLen Brown #define	BIC_PkgCnt	(1ULL << 33)
164*164d7a96SLen Brown #define	BIC_RAMWatt	(1ULL << 34)
165*164d7a96SLen Brown #define	BIC_PKG__	(1ULL << 35)
166*164d7a96SLen Brown #define	BIC_RAM__	(1ULL << 36)
167*164d7a96SLen Brown #define	BIC_Pkg_J	(1ULL << 37)
168*164d7a96SLen Brown #define	BIC_Cor_J	(1ULL << 38)
169*164d7a96SLen Brown #define	BIC_GFX_J	(1ULL << 39)
170*164d7a96SLen Brown #define	BIC_RAM_J	(1ULL << 40)
171*164d7a96SLen Brown #define	BIC_Mod_c6	(1ULL << 41)
172*164d7a96SLen Brown #define	BIC_Totl_c0	(1ULL << 42)
173*164d7a96SLen Brown #define	BIC_Any_c0	(1ULL << 43)
174*164d7a96SLen Brown #define	BIC_GFX_c0	(1ULL << 44)
175*164d7a96SLen Brown #define	BIC_CPUGFX	(1ULL << 45)
176*164d7a96SLen Brown #define	BIC_Core	(1ULL << 46)
177*164d7a96SLen Brown #define	BIC_CPU		(1ULL << 47)
178*164d7a96SLen Brown #define	BIC_APIC	(1ULL << 48)
179*164d7a96SLen Brown #define	BIC_X2APIC	(1ULL << 49)
180*164d7a96SLen Brown #define	BIC_Die		(1ULL << 50)
181*164d7a96SLen Brown #define	BIC_GFXACTMHz	(1ULL << 51)
182*164d7a96SLen Brown #define	BIC_IPC		(1ULL << 52)
183*164d7a96SLen Brown #define	BIC_CORE_THROT_CNT	(1ULL << 53)
184*164d7a96SLen Brown 
185*164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die )
186*164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__)
187*164d7a96SLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz )
188*164d7a96SLen Brown #define BIC_IDLE ( BIC_sysfs | BIC_CPU_c1 | BIC_CPU_c3 | BIC_CPU_c6 | BIC_CPU_c7 | BIC_GFX_rc6 | BIC_Pkgpc2 | BIC_Pkgpc3 | BIC_Pkgpc6 | BIC_Pkgpc7 | BIC_Pkgpc8 | BIC_Pkgpc9 | BIC_Pkgpc10 | BIC_CPU_LPI | BIC_SYS_LPI | BIC_Mod_c6 | BIC_Totl_c0 | BIC_Any_c0 | BIC_GFX_c0 | BIC_CPUGFX)
189*164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC)
190*164d7a96SLen Brown 
191*164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
192*164d7a96SLen Brown 
193*164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
194*164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
195*164d7a96SLen Brown 
196*164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
197*164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
198*164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
199*164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
200*164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
201*164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
202*164d7a96SLen Brown 
203103a8feaSLen Brown char *proc_stat = "/proc/stat";
204b7d8c148SLen Brown FILE *outf;
20536229897SLen Brown int *fd_percpu;
2062af4f9b8SLen Brown int *fd_instr_count_percpu;
207b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 };
20847936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 };
209e9d3092fSZhang Rui 
210e9d3092fSZhang Rui /* Save original CPU model */
211e9d3092fSZhang Rui unsigned int model_orig;
212e9d3092fSZhang Rui 
213023fe0acSChen Yu unsigned int num_iterations;
214c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations;
215d8af6f5fSLen Brown unsigned int debug;
21696e47158SLen Brown unsigned int quiet;
2173f44a5c6SLen Brown unsigned int shown;
2180de6c0dfSLen Brown unsigned int sums_need_wide_columns;
219d8af6f5fSLen Brown unsigned int rapl_joules;
220d8af6f5fSLen Brown unsigned int summary_only;
221c8ade361SLen Brown unsigned int list_header_only;
222d8af6f5fSLen Brown unsigned int dump_only;
223103a8feaSLen Brown unsigned int do_snb_cstates;
224fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
225144b44b1SLen Brown unsigned int do_slm_cstates;
226144b44b1SLen Brown unsigned int use_c1_residency_msr;
227103a8feaSLen Brown unsigned int has_aperf;
228889facbeSLen Brown unsigned int has_epb;
2295a63426eSLen Brown unsigned int do_irtl_snb;
2305a63426eSLen Brown unsigned int do_irtl_hsw;
231fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
232103a8feaSLen Brown unsigned int genuine_intel;
23334041551SLen Brown unsigned int authentic_amd;
234c1c10cc7SPu Wen unsigned int hygon_genuine;
23534041551SLen Brown unsigned int max_level, max_extended_level;
236103a8feaSLen Brown unsigned int has_invariant_tsc;
237d7899447SLen Brown unsigned int do_nhm_platform_info;
238cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
239b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
240103a8feaSLen Brown double bclk;
241a2b7b749SLen Brown double base_hz;
24221ed5574SLen Brown unsigned int has_base_hz;
243a2b7b749SLen Brown double tsc_tweak = 1.0;
244c98d5d94SLen Brown unsigned int show_pkg_only;
245c98d5d94SLen Brown unsigned int show_core_only;
246c98d5d94SLen Brown char *output_buffer, *outp;
247889facbeSLen Brown unsigned int do_rapl;
248889facbeSLen Brown unsigned int do_dts;
249889facbeSLen Brown unsigned int do_ptm;
2502af4f9b8SLen Brown unsigned int do_ipc;
251fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms;
252be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
253be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
25427d47356SLen Brown unsigned int gfx_cur_mhz;
255b4b91569SRafael Antognolli unsigned int gfx_act_mhz;
25655279aefSZhang Rui unsigned int tj_max;
25755279aefSZhang Rui unsigned int tj_max_override;
2580b9a0b9bSZhang Rui int tcc_offset_bits;
25940ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
26040ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
261889facbeSLen Brown double rapl_joule_counter_range;
2623a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
263ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
264aeb01e6dSChen Yu unsigned int dis_cstate_prewake;
2653a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
2663a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
2678a5bdf41SLen Brown unsigned int crystal_hz;
2688a5bdf41SLen Brown unsigned long long tsc_hz;
2697ce7d5deSPrarit Bhargava int base_cpu;
27021ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
2717f5c258eSLen Brown unsigned int has_hwp;		/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
2727f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
2737f5c258eSLen Brown unsigned int has_hwp_notify;	/* IA32_HWP_INTERRUPT */
2747f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
2757f5c258eSLen Brown unsigned int has_hwp_epp;	/* IA32_HWP_REQUEST[bits 31:24] */
2767f5c258eSLen Brown unsigned int has_hwp_pkg;	/* IA32_HWP_REQUEST_PKG */
27733148d67SLen Brown unsigned int has_misc_feature_control;
2784c2122d4SLen Brown unsigned int first_counter_read = 1;
279c026c236SArtem Bityutskiy int ignore_stdin;
280889facbeSLen Brown 
281889facbeSLen Brown #define RAPL_PKG		(1 << 0)
282e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
283e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
284e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
285e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
286e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
287e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
288e6f9bb3cSLen Brown 
289889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
290e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
291e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
292e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
293e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
2940b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
2950b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
296e6f9bb3cSLen Brown 
2979148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
298e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
2990b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
300e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
301e6f9bb3cSLen Brown 
3020b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
303e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
304e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
305e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
3069148494cSJacob Pan 
3079148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
3089148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
3099392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY	(1 << 10)
3109392bd98SCalvin Walton 					/* Indicates cores energy collection is per-core,
3119392bd98SCalvin Walton 					 * not per-package. */
3129392bd98SCalvin Walton #define RAPL_AMD_F17H		(1 << 11)
3139392bd98SCalvin Walton 					/* 0xc0010299 MSR_RAPL_PWR_UNIT */
3149392bd98SCalvin Walton 					/* 0xc001029a MSR_CORE_ENERGY_STAT */
3159392bd98SCalvin Walton 					/* 0xc001029b MSR_PKG_ENERGY_STAT */
3169148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
317889facbeSLen Brown #define	TJMAX_DEFAULT	100
318889facbeSLen Brown 
3199392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
3209392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT	0xc0010299
3219392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT	0xc001029a
3229392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT	0xc001029b
3239392bd98SCalvin Walton 
324889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
325103a8feaSLen Brown 
326103a8feaSLen Brown int backwards_count;
327103a8feaSLen Brown char *progname;
328103a8feaSLen Brown 
3291ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
3301ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
3311ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
3320748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
3330748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
3340e2d8f05SLen Brown #define BITMASK_SIZE 32
335c98d5d94SLen Brown 
336c98d5d94SLen Brown struct thread_data {
337f4fdf2b4SLen Brown 	struct timeval tv_begin;
338f4fdf2b4SLen Brown 	struct timeval tv_end;
339d4794f25SYazen Ghannam 	struct timeval tv_delta;
340c98d5d94SLen Brown 	unsigned long long tsc;
341c98d5d94SLen Brown 	unsigned long long aperf;
342c98d5d94SLen Brown 	unsigned long long mperf;
343144b44b1SLen Brown 	unsigned long long c1;
3442af4f9b8SLen Brown 	unsigned long long instr_count;
3450de6c0dfSLen Brown 	unsigned long long irq_count;
3461ed51011SLen Brown 	unsigned int smi_count;
347c98d5d94SLen Brown 	unsigned int cpu_id;
3484c2122d4SLen Brown 	unsigned int apic_id;
3494c2122d4SLen Brown 	unsigned int x2apic_id;
350c98d5d94SLen Brown 	unsigned int flags;
3517ab5ff49SZhang Rui 	bool is_atom;
352c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
353c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
3540748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
355c98d5d94SLen Brown } *thread_even, *thread_odd;
356c98d5d94SLen Brown 
357c98d5d94SLen Brown struct core_data {
358c98d5d94SLen Brown 	unsigned long long c3;
359c98d5d94SLen Brown 	unsigned long long c6;
360c98d5d94SLen Brown 	unsigned long long c7;
3610539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
362889facbeSLen Brown 	unsigned int core_temp_c;
3639392bd98SCalvin Walton 	unsigned int core_energy;	/* MSR_CORE_ENERGY_STAT */
364c98d5d94SLen Brown 	unsigned int core_id;
365eae97e05SChen Yu 	unsigned long long core_throt_cnt;
366678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
367c98d5d94SLen Brown } *core_even, *core_odd;
368c98d5d94SLen Brown 
369c98d5d94SLen Brown struct pkg_data {
370c98d5d94SLen Brown 	unsigned long long pc2;
371c98d5d94SLen Brown 	unsigned long long pc3;
372c98d5d94SLen Brown 	unsigned long long pc6;
373c98d5d94SLen Brown 	unsigned long long pc7;
374ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
375ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
376ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
377be0e54c4SLen Brown 	unsigned long long cpu_lpi;
378be0e54c4SLen Brown 	unsigned long long sys_lpi;
3790b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
3800b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
3810b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
3820b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
3839185e988SLen Brown 	long long gfx_rc6_ms;
38427d47356SLen Brown 	unsigned int gfx_mhz;
385b4b91569SRafael Antognolli 	unsigned int gfx_act_mhz;
386c98d5d94SLen Brown 	unsigned int package_id;
3877c2ccc50SChen Yu 	unsigned long long energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
3887c2ccc50SChen Yu 	unsigned long long energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
3897c2ccc50SChen Yu 	unsigned long long energy_cores;	/* MSR_PP0_ENERGY_STATUS */
3907c2ccc50SChen Yu 	unsigned long long energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
3917c2ccc50SChen Yu 	unsigned long long rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
3927c2ccc50SChen Yu 	unsigned long long rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
393889facbeSLen Brown 	unsigned int pkg_temp_c;
394678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
395c98d5d94SLen Brown } *package_even, *package_odd;
396c98d5d94SLen Brown 
397c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
398c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
399c98d5d94SLen Brown 
40040f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
40140f5cfe7SPrarit Bhargava 	((thread_base) +						      \
40240f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
40340f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
40440f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
40540f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
40640f5cfe7SPrarit Bhargava 	 (thread_no))
40740f5cfe7SPrarit Bhargava 
40840f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
40940f5cfe7SPrarit Bhargava 	((core_base) +							\
41040f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
41140f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
41240f5cfe7SPrarit Bhargava 	 (core_no))
41340f5cfe7SPrarit Bhargava 
414c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
415c98d5d94SLen Brown 
41687e15da9SChen Yu /*
41787e15da9SChen Yu  * The accumulated sum of MSR is defined as a monotonic
41887e15da9SChen Yu  * increasing MSR, it will be accumulated periodically,
41987e15da9SChen Yu  * despite its register's bit width.
42087e15da9SChen Yu  */
42187e15da9SChen Yu enum {
42287e15da9SChen Yu 	IDX_PKG_ENERGY,
42387e15da9SChen Yu 	IDX_DRAM_ENERGY,
42487e15da9SChen Yu 	IDX_PP0_ENERGY,
42587e15da9SChen Yu 	IDX_PP1_ENERGY,
42687e15da9SChen Yu 	IDX_PKG_PERF,
42787e15da9SChen Yu 	IDX_DRAM_PERF,
42887e15da9SChen Yu 	IDX_COUNT,
42987e15da9SChen Yu };
43087e15da9SChen Yu 
43187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
43287e15da9SChen Yu 
43387e15da9SChen Yu struct msr_sum_array {
43487e15da9SChen Yu 	/* get_msr_sum() = sum + (get_msr() - last) */
43587e15da9SChen Yu 	struct {
43687e15da9SChen Yu 		/*The accumulated MSR value is updated by the timer */
43787e15da9SChen Yu 		unsigned long long sum;
43887e15da9SChen Yu 		/*The MSR footprint recorded in last timer */
43987e15da9SChen Yu 		unsigned long long last;
44087e15da9SChen Yu 	} entries[IDX_COUNT];
44187e15da9SChen Yu };
44287e15da9SChen Yu 
44387e15da9SChen Yu /* The percpu MSR sum array.*/
44487e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum;
44587e15da9SChen Yu 
44613a779deSCalvin Walton off_t idx_to_offset(int idx)
44787e15da9SChen Yu {
44813a779deSCalvin Walton 	off_t offset;
44987e15da9SChen Yu 
45087e15da9SChen Yu 	switch (idx) {
45187e15da9SChen Yu 	case IDX_PKG_ENERGY:
452301b1d3aSBas Nieuwenhuizen 		if (do_rapl & RAPL_AMD_F17H)
453301b1d3aSBas Nieuwenhuizen 			offset = MSR_PKG_ENERGY_STAT;
454301b1d3aSBas Nieuwenhuizen 		else
45587e15da9SChen Yu 			offset = MSR_PKG_ENERGY_STATUS;
45687e15da9SChen Yu 		break;
45787e15da9SChen Yu 	case IDX_DRAM_ENERGY:
45887e15da9SChen Yu 		offset = MSR_DRAM_ENERGY_STATUS;
45987e15da9SChen Yu 		break;
46087e15da9SChen Yu 	case IDX_PP0_ENERGY:
46187e15da9SChen Yu 		offset = MSR_PP0_ENERGY_STATUS;
46287e15da9SChen Yu 		break;
46387e15da9SChen Yu 	case IDX_PP1_ENERGY:
46487e15da9SChen Yu 		offset = MSR_PP1_ENERGY_STATUS;
46587e15da9SChen Yu 		break;
46687e15da9SChen Yu 	case IDX_PKG_PERF:
46787e15da9SChen Yu 		offset = MSR_PKG_PERF_STATUS;
46887e15da9SChen Yu 		break;
46987e15da9SChen Yu 	case IDX_DRAM_PERF:
47087e15da9SChen Yu 		offset = MSR_DRAM_PERF_STATUS;
47187e15da9SChen Yu 		break;
47287e15da9SChen Yu 	default:
47387e15da9SChen Yu 		offset = -1;
47487e15da9SChen Yu 	}
47587e15da9SChen Yu 	return offset;
47687e15da9SChen Yu }
47787e15da9SChen Yu 
47813a779deSCalvin Walton int offset_to_idx(off_t offset)
47987e15da9SChen Yu {
48087e15da9SChen Yu 	int idx;
48187e15da9SChen Yu 
48287e15da9SChen Yu 	switch (offset) {
48387e15da9SChen Yu 	case MSR_PKG_ENERGY_STATUS:
484301b1d3aSBas Nieuwenhuizen 	case MSR_PKG_ENERGY_STAT:
48587e15da9SChen Yu 		idx = IDX_PKG_ENERGY;
48687e15da9SChen Yu 		break;
48787e15da9SChen Yu 	case MSR_DRAM_ENERGY_STATUS:
48887e15da9SChen Yu 		idx = IDX_DRAM_ENERGY;
48987e15da9SChen Yu 		break;
49087e15da9SChen Yu 	case MSR_PP0_ENERGY_STATUS:
49187e15da9SChen Yu 		idx = IDX_PP0_ENERGY;
49287e15da9SChen Yu 		break;
49387e15da9SChen Yu 	case MSR_PP1_ENERGY_STATUS:
49487e15da9SChen Yu 		idx = IDX_PP1_ENERGY;
49587e15da9SChen Yu 		break;
49687e15da9SChen Yu 	case MSR_PKG_PERF_STATUS:
49787e15da9SChen Yu 		idx = IDX_PKG_PERF;
49887e15da9SChen Yu 		break;
49987e15da9SChen Yu 	case MSR_DRAM_PERF_STATUS:
50087e15da9SChen Yu 		idx = IDX_DRAM_PERF;
50187e15da9SChen Yu 		break;
50287e15da9SChen Yu 	default:
50387e15da9SChen Yu 		idx = -1;
50487e15da9SChen Yu 	}
50587e15da9SChen Yu 	return idx;
50687e15da9SChen Yu }
50787e15da9SChen Yu 
50887e15da9SChen Yu int idx_valid(int idx)
50987e15da9SChen Yu {
51087e15da9SChen Yu 	switch (idx) {
51187e15da9SChen Yu 	case IDX_PKG_ENERGY:
512301b1d3aSBas Nieuwenhuizen 		return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
51387e15da9SChen Yu 	case IDX_DRAM_ENERGY:
51487e15da9SChen Yu 		return do_rapl & RAPL_DRAM;
51587e15da9SChen Yu 	case IDX_PP0_ENERGY:
51687e15da9SChen Yu 		return do_rapl & RAPL_CORES_ENERGY_STATUS;
51787e15da9SChen Yu 	case IDX_PP1_ENERGY:
51887e15da9SChen Yu 		return do_rapl & RAPL_GFX;
51987e15da9SChen Yu 	case IDX_PKG_PERF:
52087e15da9SChen Yu 		return do_rapl & RAPL_PKG_PERF_STATUS;
52187e15da9SChen Yu 	case IDX_DRAM_PERF:
52287e15da9SChen Yu 		return do_rapl & RAPL_DRAM_PERF_STATUS;
52387e15da9SChen Yu 	default:
52487e15da9SChen Yu 		return 0;
52587e15da9SChen Yu 	}
52687e15da9SChen Yu }
5271b439f01SLen Brown 
528388e9c81SLen Brown struct sys_counters {
529678a3bd1SLen Brown 	unsigned int added_thread_counters;
530678a3bd1SLen Brown 	unsigned int added_core_counters;
531678a3bd1SLen Brown 	unsigned int added_package_counters;
532388e9c81SLen Brown 	struct msr_counter *tp;
533388e9c81SLen Brown 	struct msr_counter *cp;
534388e9c81SLen Brown 	struct msr_counter *pp;
535388e9c81SLen Brown } sys;
536388e9c81SLen Brown 
537c98d5d94SLen Brown struct system_summary {
538c98d5d94SLen Brown 	struct thread_data threads;
539c98d5d94SLen Brown 	struct core_data cores;
540c98d5d94SLen Brown 	struct pkg_data packages;
541388e9c81SLen Brown } average;
542c98d5d94SLen Brown 
5430e2d8f05SLen Brown struct cpu_topology {
5440e2d8f05SLen Brown 	int physical_package_id;
5456de68fe1SLen Brown 	int die_id;
5460e2d8f05SLen Brown 	int logical_cpu_id;
547ef605741SPrarit Bhargava 	int physical_node_id;
548ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
5490e2d8f05SLen Brown 	int physical_core_id;
5508cb48b32SPrarit Bhargava 	int thread_id;
5510e2d8f05SLen Brown 	cpu_set_t *put_ids;	/* Processing Unit/Thread IDs */
5520e2d8f05SLen Brown } *cpus;
553c98d5d94SLen Brown 
554c98d5d94SLen Brown struct topo_params {
555c98d5d94SLen Brown 	int num_packages;
5566de68fe1SLen Brown 	int num_die;
557103a8feaSLen Brown 	int num_cpus;
558c98d5d94SLen Brown 	int num_cores;
559c98d5d94SLen Brown 	int max_cpu_num;
560ef605741SPrarit Bhargava 	int max_node_num;
56170a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
56270a9c6e8SPrarit Bhargava 	int cores_per_node;
56370a9c6e8SPrarit Bhargava 	int threads_per_core;
564c98d5d94SLen Brown } topo;
565103a8feaSLen Brown 
566c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
567103a8feaSLen Brown 
568562a2d37SLen Brown int *irq_column_2_cpu;		/* /proc/interrupts column numbers */
569562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
570562a2d37SLen Brown 
571c98d5d94SLen Brown void setup_all_buffers(void);
572103a8feaSLen Brown 
5731f81c5efSLen Brown char *sys_lpi_file;
5741f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
5751f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
5761f81c5efSLen Brown 
577c98d5d94SLen Brown int cpu_is_not_present(int cpu)
578d15cf7c1SLen Brown {
579c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
580c98d5d94SLen Brown }
5811b439f01SLen Brown 
582c98d5d94SLen Brown /*
583c98d5d94SLen Brown  * run func(thread, core, package) in topology order
584c98d5d94SLen Brown  * skip non-present cpus
585c98d5d94SLen Brown  */
586c98d5d94SLen Brown 
587c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *),
588c98d5d94SLen Brown 		 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
589c98d5d94SLen Brown {
59040f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
591c98d5d94SLen Brown 
592c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
593df2f677dSLen Brown 		for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
59470a9c6e8SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
5951b439f01SLen Brown 				for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
596c98d5d94SLen Brown 					struct thread_data *t;
597c98d5d94SLen Brown 					struct core_data *c;
598c98d5d94SLen Brown 					struct pkg_data *p;
599c98d5d94SLen Brown 
6001b439f01SLen Brown 					t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
601c98d5d94SLen Brown 
602c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
603c98d5d94SLen Brown 						continue;
604c98d5d94SLen Brown 
6051b439f01SLen Brown 					c = GET_CORE(core_base, core_no, node_no, pkg_no);
606c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
607c98d5d94SLen Brown 
608c98d5d94SLen Brown 					retval = func(t, c, p);
609c98d5d94SLen Brown 					if (retval)
610c98d5d94SLen Brown 						return retval;
611c98d5d94SLen Brown 				}
612c98d5d94SLen Brown 			}
613c98d5d94SLen Brown 		}
61440f5cfe7SPrarit Bhargava 	}
615d15cf7c1SLen Brown 	return 0;
616d15cf7c1SLen Brown }
617d15cf7c1SLen Brown 
61888c3281fSLen Brown int cpu_migrate(int cpu)
61988c3281fSLen Brown {
620c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
621c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
622c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
62388c3281fSLen Brown 		return -1;
62488c3281fSLen Brown 	else
62588c3281fSLen Brown 		return 0;
62688c3281fSLen Brown }
6271b439f01SLen Brown 
62836229897SLen Brown int get_msr_fd(int cpu)
629103a8feaSLen Brown {
630103a8feaSLen Brown 	char pathname[32];
631103a8feaSLen Brown 	int fd;
632103a8feaSLen Brown 
63336229897SLen Brown 	fd = fd_percpu[cpu];
63436229897SLen Brown 
63536229897SLen Brown 	if (fd)
63636229897SLen Brown 		return fd;
63736229897SLen Brown 
638103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
639103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
64015aaa346SLen Brown 	if (fd < 0)
64198481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
642103a8feaSLen Brown 
64336229897SLen Brown 	fd_percpu[cpu] = fd;
64436229897SLen Brown 
64536229897SLen Brown 	return fd;
64636229897SLen Brown }
64736229897SLen Brown 
6482af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
6492af4f9b8SLen Brown {
6502af4f9b8SLen Brown 	return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
6512af4f9b8SLen Brown }
6522af4f9b8SLen Brown 
6532af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num)
6542af4f9b8SLen Brown {
6552af4f9b8SLen Brown 	struct perf_event_attr pea;
6562af4f9b8SLen Brown 	int fd;
6572af4f9b8SLen Brown 
6582af4f9b8SLen Brown 	memset(&pea, 0, sizeof(struct perf_event_attr));
6592af4f9b8SLen Brown 	pea.type = PERF_TYPE_HARDWARE;
6602af4f9b8SLen Brown 	pea.size = sizeof(struct perf_event_attr);
6612af4f9b8SLen Brown 	pea.config = PERF_COUNT_HW_INSTRUCTIONS;
6622af4f9b8SLen Brown 
6632af4f9b8SLen Brown 	/* counter for cpu_num, including user + kernel and all processes */
6642af4f9b8SLen Brown 	fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
665*164d7a96SLen Brown 	if (fd == -1) {
666*164d7a96SLen Brown 		warn("cpu%d: perf instruction counter", cpu_num);
667*164d7a96SLen Brown 		BIC_NOT_PRESENT(BIC_IPC);
668*164d7a96SLen Brown 	}
6692af4f9b8SLen Brown 
6702af4f9b8SLen Brown 	return fd;
6712af4f9b8SLen Brown }
6722af4f9b8SLen Brown 
6732af4f9b8SLen Brown int get_instr_count_fd(int cpu)
6742af4f9b8SLen Brown {
6752af4f9b8SLen Brown 	if (fd_instr_count_percpu[cpu])
6762af4f9b8SLen Brown 		return fd_instr_count_percpu[cpu];
6772af4f9b8SLen Brown 
6782af4f9b8SLen Brown 	fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
6792af4f9b8SLen Brown 
6802af4f9b8SLen Brown 	return fd_instr_count_percpu[cpu];
6812af4f9b8SLen Brown }
6822af4f9b8SLen Brown 
68336229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
68436229897SLen Brown {
68536229897SLen Brown 	ssize_t retval;
68636229897SLen Brown 
68736229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
68815aaa346SLen Brown 
68998481e79SLen Brown 	if (retval != sizeof *msr)
690cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
69115aaa346SLen Brown 
69215aaa346SLen Brown 	return 0;
693103a8feaSLen Brown }
694103a8feaSLen Brown 
695dd778a5eSLen Brown #define MAX_DEFERRED 16
6960fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED];
697dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
6980fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index;
699dd778a5eSLen Brown int deferred_skip_index;
700dd778a5eSLen Brown 
701dd778a5eSLen Brown /*
702dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
703dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
704dd778a5eSLen Brown  */
705dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
706dd778a5eSLen Brown 
707dd778a5eSLen Brown void help(void)
708dd778a5eSLen Brown {
709dd778a5eSLen Brown 	fprintf(outf,
710dd778a5eSLen Brown 		"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
711dd778a5eSLen Brown 		"\n"
712dd778a5eSLen Brown 		"Turbostat forks the specified COMMAND and prints statistics\n"
713dd778a5eSLen Brown 		"when COMMAND completes.\n"
714dd778a5eSLen Brown 		"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
715dd778a5eSLen Brown 		"to print statistics, until interrupted.\n"
716cc481650SNathan Ciobanu 		"  -a, --add	add a counter\n"
717dd778a5eSLen Brown 		"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
718cc481650SNathan Ciobanu 		"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
719dd778a5eSLen Brown 		"		  {core | package | j,k,l..m,n-p }\n"
7209ce80578SNathan Ciobanu 		"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
7219ce80578SNathan Ciobanu 		"  -D, --Dump	displays the raw counter values\n"
7229ce80578SNathan Ciobanu 		"  -e, --enable	[all | column]\n"
7239ce80578SNathan Ciobanu 		"		shows all or the specified disabled column\n"
7249ce80578SNathan Ciobanu 		"  -H, --hide [column|column,column,...]\n"
7259ce80578SNathan Ciobanu 		"		hide the specified column(s)\n"
726cc481650SNathan Ciobanu 		"  -i, --interval sec.subsec\n"
727cc481650SNathan Ciobanu 		"		Override default 5-second measurement interval\n"
7289ce80578SNathan Ciobanu 		"  -J, --Joules	displays energy in Joules instead of Watts\n"
729cc481650SNathan Ciobanu 		"  -l, --list	list column headers only\n"
730cc481650SNathan Ciobanu 		"  -n, --num_iterations num\n"
731cc481650SNathan Ciobanu 		"		number of the measurement iterations\n"
732c7e399f8SZephaniah E. Loss-Cutler-Hull 		"  -N, --header_iterations num\n"
733c7e399f8SZephaniah E. Loss-Cutler-Hull 		"		print header every num iterations\n"
734cc481650SNathan Ciobanu 		"  -o, --out file\n"
735cc481650SNathan Ciobanu 		"		create or truncate \"file\" for all output\n"
736cc481650SNathan Ciobanu 		"  -q, --quiet	skip decoding system configuration header\n"
7379ce80578SNathan Ciobanu 		"  -s, --show [column|column,column,...]\n"
7389ce80578SNathan Ciobanu 		"		show only the specified column(s)\n"
7399ce80578SNathan Ciobanu 		"  -S, --Summary\n"
7409ce80578SNathan Ciobanu 		"		limits output to 1-line system summary per interval\n"
7419ce80578SNathan Ciobanu 		"  -T, --TCC temperature\n"
7429ce80578SNathan Ciobanu 		"		sets the Thermal Control Circuit temperature in\n"
7439ce80578SNathan Ciobanu 		"		  degrees Celsius\n"
744cc481650SNathan Ciobanu 		"  -h, --help	print this help message\n"
7451b439f01SLen Brown 		"  -v, --version	print version information\n" "\n" "For more help, run \"man turbostat\"\n");
746dd778a5eSLen Brown }
747dd778a5eSLen Brown 
748812db3f7SLen Brown /*
749812db3f7SLen Brown  * bic_lookup
750812db3f7SLen Brown  * for all the strings in comma separate name_list,
751812db3f7SLen Brown  * set the approprate bit in return value.
752812db3f7SLen Brown  */
753dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
754812db3f7SLen Brown {
755812db3f7SLen Brown 	int i;
756812db3f7SLen Brown 	unsigned long long retval = 0;
757812db3f7SLen Brown 
758812db3f7SLen Brown 	while (name_list) {
759812db3f7SLen Brown 		char *comma;
760812db3f7SLen Brown 
761812db3f7SLen Brown 		comma = strchr(name_list, ',');
762812db3f7SLen Brown 
763812db3f7SLen Brown 		if (comma)
764812db3f7SLen Brown 			*comma = '\0';
765812db3f7SLen Brown 
766812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
767812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
768812db3f7SLen Brown 				retval |= (1ULL << i);
769812db3f7SLen Brown 				break;
770812db3f7SLen Brown 			}
7715dc241f2SLen Brown 			if (!strcmp(name_list, "all")) {
7725dc241f2SLen Brown 				retval |= ~0;
7735dc241f2SLen Brown 				break;
7745dc241f2SLen Brown 			} else if (!strcmp(name_list, "topology")) {
7755dc241f2SLen Brown 				retval |= BIC_TOPOLOGY;
7765dc241f2SLen Brown 				break;
7775dc241f2SLen Brown 			} else if (!strcmp(name_list, "power")) {
7785dc241f2SLen Brown 				retval |= BIC_THERMAL_PWR;
7795dc241f2SLen Brown 				break;
7805dc241f2SLen Brown 			} else if (!strcmp(name_list, "idle")) {
7815dc241f2SLen Brown 				retval |= BIC_IDLE;
7825dc241f2SLen Brown 				break;
7835dc241f2SLen Brown 			} else if (!strcmp(name_list, "frequency")) {
7845dc241f2SLen Brown 				retval |= BIC_FREQUENCY;
7855dc241f2SLen Brown 				break;
7865dc241f2SLen Brown 			} else if (!strcmp(name_list, "other")) {
7875dc241f2SLen Brown 				retval |= BIC_OTHER;
7885dc241f2SLen Brown 				break;
7895dc241f2SLen Brown 			}
7905dc241f2SLen Brown 
791812db3f7SLen Brown 		}
792812db3f7SLen Brown 		if (i == MAX_BIC) {
793dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
7940fc521bcSZephaniah E. Loss-Cutler-Hull 				deferred_add_names[deferred_add_index++] = name_list;
7950fc521bcSZephaniah E. Loss-Cutler-Hull 				if (deferred_add_index >= MAX_DEFERRED) {
7960fc521bcSZephaniah E. Loss-Cutler-Hull 					fprintf(stderr, "More than max %d un-recognized --add options '%s'\n",
7970fc521bcSZephaniah E. Loss-Cutler-Hull 						MAX_DEFERRED, name_list);
7980fc521bcSZephaniah E. Loss-Cutler-Hull 					help();
7990fc521bcSZephaniah E. Loss-Cutler-Hull 					exit(1);
800812db3f7SLen Brown 				}
8010fc521bcSZephaniah E. Loss-Cutler-Hull 			} else {
802dd778a5eSLen Brown 				deferred_skip_names[deferred_skip_index++] = name_list;
803dd778a5eSLen Brown 				if (debug)
804dd778a5eSLen Brown 					fprintf(stderr, "deferred \"%s\"\n", name_list);
805dd778a5eSLen Brown 				if (deferred_skip_index >= MAX_DEFERRED) {
806dd778a5eSLen Brown 					fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
807dd778a5eSLen Brown 						MAX_DEFERRED, name_list);
808dd778a5eSLen Brown 					help();
809dd778a5eSLen Brown 					exit(1);
810dd778a5eSLen Brown 				}
811dd778a5eSLen Brown 			}
8120fc521bcSZephaniah E. Loss-Cutler-Hull 		}
813812db3f7SLen Brown 
814812db3f7SLen Brown 		name_list = comma;
815812db3f7SLen Brown 		if (name_list)
816812db3f7SLen Brown 			name_list++;
817812db3f7SLen Brown 
818812db3f7SLen Brown 	}
819812db3f7SLen Brown 	return retval;
820812db3f7SLen Brown }
821fc04cc67SLen Brown 
822c8ade361SLen Brown void print_header(char *delim)
823103a8feaSLen Brown {
824388e9c81SLen Brown 	struct msr_counter *mp;
8256168c2e0SLen Brown 	int printed = 0;
826388e9c81SLen Brown 
8273f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
8283f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
8293f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8303f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
831812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
8326168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
8336de68fe1SLen Brown 	if (DO_BIC(BIC_Die))
8346de68fe1SLen Brown 		outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
83501235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
83601235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
837812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
8386168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
839812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
8406168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
8414c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
8424c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
8434c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
8444c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
845812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
8466168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
847812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
8486168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
849812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
8506168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
851812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
8526168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
8531cc21f7bSLen Brown 
8542af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
8552af4f9b8SLen Brown 		outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
8562af4f9b8SLen Brown 
8570de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
8580de6c0dfSLen Brown 		if (sums_need_wide_columns)
8596168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
8600de6c0dfSLen Brown 		else
8616168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
8620de6c0dfSLen Brown 	}
8630de6c0dfSLen Brown 
864812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
8656168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
8661cc21f7bSLen Brown 
867388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
868dd778a5eSLen Brown 
869388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
870388e9c81SLen Brown 			if (mp->width == 64)
871dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
872388e9c81SLen Brown 			else
873dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
874388e9c81SLen Brown 		} else {
8750de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
876dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
8770de6c0dfSLen Brown 			else
878dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
879388e9c81SLen Brown 		}
880388e9c81SLen Brown 	}
881388e9c81SLen Brown 
88241618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
8836168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
884562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
8856168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
886812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
8876168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
888812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
8896168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
890678a3bd1SLen Brown 
8910539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
8926168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
893678a3bd1SLen Brown 
894812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
8956168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
896388e9c81SLen Brown 
897eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
898eae97e05SChen Yu 		outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : ""));
899eae97e05SChen Yu 
9009392bd98SCalvin Walton 	if (do_rapl && !rapl_joules) {
9019392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
9029392bd98SCalvin Walton 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
9039392bd98SCalvin Walton 	} else if (do_rapl && rapl_joules) {
9049392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
9059392bd98SCalvin Walton 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
9069392bd98SCalvin Walton 	}
9079392bd98SCalvin Walton 
908388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
909388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
910388e9c81SLen Brown 			if (mp->width == 64)
911c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
912388e9c81SLen Brown 			else
913c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
914388e9c81SLen Brown 		} else {
9150de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9160de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
9170de6c0dfSLen Brown 			else
918c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
919388e9c81SLen Brown 		}
920388e9c81SLen Brown 	}
921388e9c81SLen Brown 
922812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
9236168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
924889facbeSLen Brown 
925812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
9266168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
927fdf676e5SLen Brown 
928812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
9296168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
93027d47356SLen Brown 
931b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
932b4b91569SRafael Antognolli 		outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
933b4b91569SRafael Antognolli 
934a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
9356168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
936a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
9376168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
938a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
9396168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
940a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
9416168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
9420b2bb692SLen Brown 
9430f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
9446168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
9450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
9466168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
9470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
9486168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
9490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
9506168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
9510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
9526168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
9530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
9546168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
9550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
9566168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
957be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
958be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
959be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
960be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
961103a8feaSLen Brown 
9625c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
963812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
9646168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
9659392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9666168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
967812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
9686168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
969812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
9706168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
971812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
9726168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
973812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
9746168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
975d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
976812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
9776168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
9789392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9796168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
980812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
9816168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
982812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
9836168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
984812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
9856168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
986812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
9876168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
9885c56be9aSDirk Brandewie 	}
989388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
990388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
991388e9c81SLen Brown 			if (mp->width == 64)
992c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
993388e9c81SLen Brown 			else
994c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
995388e9c81SLen Brown 		} else {
9960de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9970de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
9980de6c0dfSLen Brown 			else
999c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
1000388e9c81SLen Brown 		}
1001388e9c81SLen Brown 	}
1002388e9c81SLen Brown 
1003c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
1004103a8feaSLen Brown }
1005103a8feaSLen Brown 
10061b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1007103a8feaSLen Brown {
1008388e9c81SLen Brown 	int i;
1009388e9c81SLen Brown 	struct msr_counter *mp;
1010388e9c81SLen Brown 
10113b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
1012c98d5d94SLen Brown 
1013c98d5d94SLen Brown 	if (t) {
10141b439f01SLen Brown 		outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
10153b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
10163b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
10173b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
10183b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
10196886fee4SLen Brown 
10202af4f9b8SLen Brown 		if (DO_BIC(BIC_IPC))
10212af4f9b8SLen Brown 			outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
10222af4f9b8SLen Brown 
1023812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
10240de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
1025812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
1026218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
1027388e9c81SLen Brown 
1028388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
10291b439f01SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]);
1030388e9c81SLen Brown 		}
1031103a8feaSLen Brown 	}
1032103a8feaSLen Brown 
1033c98d5d94SLen Brown 	if (c) {
10343b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
10353b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
10363b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
10373b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
10383b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1039eae97e05SChen Yu 		outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt);
10409392bd98SCalvin Walton 		outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1041388e9c81SLen Brown 
1042388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
10431b439f01SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]);
1044388e9c81SLen Brown 		}
10450539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1046c98d5d94SLen Brown 	}
1047103a8feaSLen Brown 
1048c98d5d94SLen Brown 	if (p) {
10493b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
10500b2bb692SLen Brown 
10510b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
10520b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
10530b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
10540b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
10550b2bb692SLen Brown 
10563b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
10570f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
10583b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
10590f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
10603b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
10610f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
10623b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
10633b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
10643b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
10653b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1066be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1067be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
10687c2ccc50SChen Yu 		outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
10697c2ccc50SChen Yu 		outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
10707c2ccc50SChen Yu 		outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
10717c2ccc50SChen Yu 		outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
10721b439f01SLen Brown 		outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status);
10731b439f01SLen Brown 		outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status);
10743b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1075388e9c81SLen Brown 
1076388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
10771b439f01SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]);
1078388e9c81SLen Brown 		}
1079c98d5d94SLen Brown 	}
10803b4d5c7fSAndy Shevchenko 
10813b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
10823b4d5c7fSAndy Shevchenko 
1083c98d5d94SLen Brown 	return 0;
1084103a8feaSLen Brown }
1085103a8feaSLen Brown 
1086e23da037SLen Brown /*
1087e23da037SLen Brown  * column formatting convention & formats
1088e23da037SLen Brown  */
10891b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1090103a8feaSLen Brown {
1091008d396eSLen Brown 	double interval_float, tsc;
1092fc04cc67SLen Brown 	char *fmt8;
1093388e9c81SLen Brown 	int i;
1094388e9c81SLen Brown 	struct msr_counter *mp;
10956168c2e0SLen Brown 	char *delim = "\t";
10966168c2e0SLen Brown 	int printed = 0;
1097103a8feaSLen Brown 
1098c98d5d94SLen Brown 	/* if showing only 1st thread in core and this isn't one, bail out */
1099c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1100c98d5d94SLen Brown 		return 0;
1101c98d5d94SLen Brown 
1102c98d5d94SLen Brown 	/* if showing only 1st thread in pkg and this isn't one, bail out */
1103c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1104c98d5d94SLen Brown 		return 0;
1105c98d5d94SLen Brown 
11061ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
11071b439f01SLen Brown 	if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
11081ef7d21aSLen Brown 		return 0;
11091ef7d21aSLen Brown 
11103f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
1111f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
1112f4fdf2b4SLen Brown 		struct timeval tv;
1113f4fdf2b4SLen Brown 
1114f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
1115f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1116f4fdf2b4SLen Brown 	}
1117f4fdf2b4SLen Brown 
11183f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
11193f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
11203f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
11213f44a5c6SLen Brown 
1122d4794f25SYazen Ghannam 	interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0;
1123103a8feaSLen Brown 
1124008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
1125008d396eSLen Brown 
1126c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
1127c98d5d94SLen Brown 	if (t == &average.threads) {
1128812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
11296168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11306de68fe1SLen Brown 		if (DO_BIC(BIC_Die))
11316de68fe1SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
113201235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
113301235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1134812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
11356168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1136812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
11376168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11384c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
11394c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11404c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
11414c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1142103a8feaSLen Brown 	} else {
1143812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
1144c98d5d94SLen Brown 			if (p)
11456168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1146c98d5d94SLen Brown 			else
11476168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1148c98d5d94SLen Brown 		}
11496de68fe1SLen Brown 		if (DO_BIC(BIC_Die)) {
11506de68fe1SLen Brown 			if (c)
11516de68fe1SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
11526de68fe1SLen Brown 			else
11536de68fe1SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11546de68fe1SLen Brown 		}
115501235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
115601235041SPrarit Bhargava 			if (t)
115701235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
11581b439f01SLen Brown 						(printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id);
115901235041SPrarit Bhargava 			else
11601b439f01SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
116101235041SPrarit Bhargava 		}
1162812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
1163c98d5d94SLen Brown 			if (c)
11646168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1165c98d5d94SLen Brown 			else
11666168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1167c98d5d94SLen Brown 		}
1168812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
11696168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
11704c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
11714c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
11724c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
11734c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1174103a8feaSLen Brown 	}
1175fc04cc67SLen Brown 
1176812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
11771b439f01SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
1178fc04cc67SLen Brown 
1179812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
11806168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
1181103a8feaSLen Brown 
1182812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
118321ed5574SLen Brown 		if (has_base_hz)
11841b439f01SLen Brown 			outp +=
11851b439f01SLen Brown 			    sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
118621ed5574SLen Brown 		else
11876168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1188008d396eSLen Brown 					tsc / units * t->aperf / t->mperf / interval_float);
118921ed5574SLen Brown 	}
1190103a8feaSLen Brown 
1191812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
11926168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float);
1193103a8feaSLen Brown 
11942af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
11952af4f9b8SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
11962af4f9b8SLen Brown 
1197562a2d37SLen Brown 	/* IRQ */
11980de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
11990de6c0dfSLen Brown 		if (sums_need_wide_columns)
12006168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
12010de6c0dfSLen Brown 		else
12026168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
12030de6c0dfSLen Brown 	}
1204562a2d37SLen Brown 
12051cc21f7bSLen Brown 	/* SMI */
1206812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
12076168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
12081cc21f7bSLen Brown 
1209678a3bd1SLen Brown 	/* Added counters */
1210678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1211678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
1212678a3bd1SLen Brown 			if (mp->width == 32)
12131b439f01SLen Brown 				outp +=
12141b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]);
1215678a3bd1SLen Brown 			else
12166168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1217678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
12180de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12196168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
12200de6c0dfSLen Brown 			else
12216168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1222678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
122341618e63SLen Brown 			if (mp->type == COUNTER_USEC)
12241b439f01SLen Brown 				outp +=
12251b439f01SLen Brown 				    sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
12261b439f01SLen Brown 					    t->counter[i] / interval_float / 10000);
122741618e63SLen Brown 			else
12286168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc);
1229678a3bd1SLen Brown 		}
1230678a3bd1SLen Brown 	}
1231678a3bd1SLen Brown 
123241618e63SLen Brown 	/* C1 */
123341618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
12346168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
123541618e63SLen Brown 
1236c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1237c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1238c98d5d94SLen Brown 		goto done;
1239c98d5d94SLen Brown 
1240562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
12416168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
1242812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
12436168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
1244812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
12456168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
1246c98d5d94SLen Brown 
12470539ba11SLen Brown 	/* Mod%c6 */
12480539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
12496168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
12500539ba11SLen Brown 
1251812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
12526168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1253889facbeSLen Brown 
1254eae97e05SChen Yu 	/* Core throttle count */
1255eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
1256eae97e05SChen Yu 		outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt);
1257eae97e05SChen Yu 
1258388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1259388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1260388e9c81SLen Brown 			if (mp->width == 32)
12611b439f01SLen Brown 				outp +=
12621b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]);
1263388e9c81SLen Brown 			else
12646168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1265388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
12660de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12676168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
12680de6c0dfSLen Brown 			else
12696168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1270388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
12716168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc);
1272388e9c81SLen Brown 		}
1273388e9c81SLen Brown 	}
1274388e9c81SLen Brown 
12759392bd98SCalvin Walton 	fmt8 = "%s%.2f";
12769392bd98SCalvin Walton 
12779392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
12781b439f01SLen Brown 		outp +=
12791b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
12809392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
12819392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
12829392bd98SCalvin Walton 
1283c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1284c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1285c98d5d94SLen Brown 		goto done;
1286c98d5d94SLen Brown 
12870b2bb692SLen Brown 	/* PkgTmp */
1288812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
12896168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1290889facbeSLen Brown 
1291fdf676e5SLen Brown 	/* GFXrc6 */
1292812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1293ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
12946168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
12959185e988SLen Brown 		} else {
12966168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
12979185e988SLen Brown 					p->gfx_rc6_ms / 10.0 / interval_float);
12989185e988SLen Brown 		}
12999185e988SLen Brown 	}
1300fdf676e5SLen Brown 
130127d47356SLen Brown 	/* GFXMHz */
1302812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
13036168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
130427d47356SLen Brown 
1305b4b91569SRafael Antognolli 	/* GFXACTMHz */
1306b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
1307b4b91569SRafael Antognolli 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1308b4b91569SRafael Antognolli 
13090b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1310a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13116168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
1312a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13136168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
1314a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13156168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
1316a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13176168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
13180b2bb692SLen Brown 
13190f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
13206168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
13210f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
13226168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
13230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
13246168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
13250f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
13266168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
13270f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
13286168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
13290f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
13306168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
13310f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
13326168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
1333889facbeSLen Brown 
1334be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
13351b439f01SLen Brown 		outp +=
13361b439f01SLen Brown 		    sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1337be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
13381b439f01SLen Brown 		outp +=
13391b439f01SLen Brown 		    sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1340be0e54c4SLen Brown 
1341812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
13421b439f01SLen Brown 		outp +=
13431b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1344eae97e05SChen Yu 
13459392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13461b439f01SLen Brown 		outp +=
13471b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1348812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
13491b439f01SLen Brown 		outp +=
13501b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1351812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
13521b439f01SLen Brown 		outp +=
13531b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13541b439f01SLen Brown 			    p->energy_dram * rapl_dram_energy_units / interval_float);
1355812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
13566168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
13579392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13586168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1359812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
13606168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1361812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
13626168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1363812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
13641b439f01SLen Brown 		outp +=
13651b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13661b439f01SLen Brown 			    100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1367812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
13681b439f01SLen Brown 		outp +=
13691b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13701b439f01SLen Brown 			    100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1371812db3f7SLen Brown 
1372388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1373388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1374388e9c81SLen Brown 			if (mp->width == 32)
13751b439f01SLen Brown 				outp +=
13761b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]);
1377388e9c81SLen Brown 			else
13786168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1379388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
13800de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
13816168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
13820de6c0dfSLen Brown 			else
13836168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1384388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
13856168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc);
1386388e9c81SLen Brown 		}
1387388e9c81SLen Brown 	}
1388388e9c81SLen Brown 
1389c98d5d94SLen Brown done:
139094d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1391c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1392c98d5d94SLen Brown 
1393c98d5d94SLen Brown 	return 0;
1394103a8feaSLen Brown }
1395103a8feaSLen Brown 
1396b7d8c148SLen Brown void flush_output_stdout(void)
1397103a8feaSLen Brown {
1398b7d8c148SLen Brown 	FILE *filep;
1399b7d8c148SLen Brown 
1400b7d8c148SLen Brown 	if (outf == stderr)
1401b7d8c148SLen Brown 		filep = stdout;
1402b7d8c148SLen Brown 	else
1403b7d8c148SLen Brown 		filep = outf;
1404b7d8c148SLen Brown 
1405b7d8c148SLen Brown 	fputs(output_buffer, filep);
1406b7d8c148SLen Brown 	fflush(filep);
1407b7d8c148SLen Brown 
1408c98d5d94SLen Brown 	outp = output_buffer;
1409c98d5d94SLen Brown }
14101b439f01SLen Brown 
1411b7d8c148SLen Brown void flush_output_stderr(void)
1412c98d5d94SLen Brown {
1413b7d8c148SLen Brown 	fputs(output_buffer, outf);
1414b7d8c148SLen Brown 	fflush(outf);
1415c98d5d94SLen Brown 	outp = output_buffer;
1416c98d5d94SLen Brown }
14171b439f01SLen Brown 
1418c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1419c98d5d94SLen Brown {
1420c7e399f8SZephaniah E. Loss-Cutler-Hull 	static int count;
1421103a8feaSLen Brown 
1422c7e399f8SZephaniah E. Loss-Cutler-Hull 	if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only)
1423c8ade361SLen Brown 		print_header("\t");
1424103a8feaSLen Brown 
14259d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1426103a8feaSLen Brown 
1427c7e399f8SZephaniah E. Loss-Cutler-Hull 	count++;
1428e23da037SLen Brown 
1429e23da037SLen Brown 	if (summary_only)
1430e23da037SLen Brown 		return;
1431e23da037SLen Brown 
1432c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1433103a8feaSLen Brown }
1434103a8feaSLen Brown 
1435889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
14367c2ccc50SChen Yu 	old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1437889facbeSLen Brown 
14381b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old)
1439103a8feaSLen Brown {
1440388e9c81SLen Brown 	int i;
1441388e9c81SLen Brown 	struct msr_counter *mp;
14420b2bb692SLen Brown 
1443a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14440b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1445a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14460b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1447a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14480b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1449a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14500b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1451a99d8730SLen Brown 
1452c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
14530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1454c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
14550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1456c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
14570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1458c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1459ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1460ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1461ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1462be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1463be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1464889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1465889facbeSLen Brown 
14669185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
14679185e988SLen Brown 	if (old->gfx_rc6_ms > new->gfx_rc6_ms)
14689185e988SLen Brown 		old->gfx_rc6_ms = -1;
14699185e988SLen Brown 	else
1470fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
14719185e988SLen Brown 
147227d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
1473b4b91569SRafael Antognolli 	old->gfx_act_mhz = new->gfx_act_mhz;
147427d47356SLen Brown 
147587e15da9SChen Yu 	old->energy_pkg = new->energy_pkg - old->energy_pkg;
147687e15da9SChen Yu 	old->energy_cores = new->energy_cores - old->energy_cores;
147787e15da9SChen Yu 	old->energy_gfx = new->energy_gfx - old->energy_gfx;
147887e15da9SChen Yu 	old->energy_dram = new->energy_dram - old->energy_dram;
147987e15da9SChen Yu 	old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
148087e15da9SChen Yu 	old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1481ba3dec99SLen Brown 
1482388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1483388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1484388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1485388e9c81SLen Brown 		else
1486388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1487388e9c81SLen Brown 	}
1488388e9c81SLen Brown 
1489ba3dec99SLen Brown 	return 0;
1490103a8feaSLen Brown }
1491103a8feaSLen Brown 
14921b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old)
1493c98d5d94SLen Brown {
1494388e9c81SLen Brown 	int i;
1495388e9c81SLen Brown 	struct msr_counter *mp;
1496388e9c81SLen Brown 
1497c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1498c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1499c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1500889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
1501eae97e05SChen Yu 	old->core_throt_cnt = new->core_throt_cnt;
15020539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1503388e9c81SLen Brown 
15049392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
15059392bd98SCalvin Walton 
1506388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1507388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1508388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1509388e9c81SLen Brown 		else
1510388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1511388e9c81SLen Brown 	}
1512103a8feaSLen Brown }
1513c98d5d94SLen Brown 
15141e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic)
15151e9042b9SSrinivas Pandruvada {
15161e9042b9SSrinivas Pandruvada 	if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
15171e9042b9SSrinivas Pandruvada 		return 0;
15181e9042b9SSrinivas Pandruvada 
15191e9042b9SSrinivas Pandruvada 	return DO_BIC_READ(bic);
15201e9042b9SSrinivas Pandruvada }
15211e9042b9SSrinivas Pandruvada 
1522c3ae331dSLen Brown /*
1523c3ae331dSLen Brown  * old = new - old
1524c3ae331dSLen Brown  */
15251b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta)
1526c98d5d94SLen Brown {
1527388e9c81SLen Brown 	int i;
1528388e9c81SLen Brown 	struct msr_counter *mp;
1529388e9c81SLen Brown 
15304c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
15314c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
15324c2122d4SLen Brown 		new->apic_id = old->apic_id;
15334c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
15344c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
15354c2122d4SLen Brown 
15363f44a5c6SLen Brown 	/*
15373f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
15383f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
15393f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
15403f44a5c6SLen Brown 	 */
15413f44a5c6SLen Brown 
1542d4794f25SYazen Ghannam 	timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
15433f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
15443f44a5c6SLen Brown 	old->tv_end = new->tv_end;
15453f44a5c6SLen Brown 
1546c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1547c98d5d94SLen Brown 
1548103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1549b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1550b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1551b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1552b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1553103a8feaSLen Brown 
1554c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1555c98d5d94SLen Brown 
15561b439f01SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
1557c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1558c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1559c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1560c98d5d94SLen Brown 		} else {
1561ba3dec99SLen Brown 			return -1;
1562103a8feaSLen Brown 		}
1563a729617cSLen Brown 	}
1564103a8feaSLen Brown 
1565144b44b1SLen Brown 	if (use_c1_residency_msr) {
1566144b44b1SLen Brown 		/*
1567144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1568144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1569144b44b1SLen Brown 		 */
1570144b44b1SLen Brown 	} else {
1571103a8feaSLen Brown 		/*
1572c3ae331dSLen Brown 		 * As counter collection is not atomic,
1573c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1574103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1575103a8feaSLen Brown 		 */
157695149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1577c98d5d94SLen Brown 			old->c1 = 0;
1578c98d5d94SLen Brown 		else {
1579c98d5d94SLen Brown 			/* normal case, derive c1 */
1580008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1581c98d5d94SLen Brown 			    - core_delta->c6 - core_delta->c7;
1582c98d5d94SLen Brown 		}
1583144b44b1SLen Brown 	}
1584c3ae331dSLen Brown 
1585c98d5d94SLen Brown 	if (old->mperf == 0) {
1586b7d8c148SLen Brown 		if (debug > 1)
1587b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1588c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1589c98d5d94SLen Brown 	}
1590103a8feaSLen Brown 
15912af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
15922af4f9b8SLen Brown 		old->instr_count = new->instr_count - old->instr_count;
15932af4f9b8SLen Brown 
1594812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1595562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1596562a2d37SLen Brown 
1597812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
15981ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1599ba3dec99SLen Brown 
1600388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1601388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1602388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1603388e9c81SLen Brown 		else
1604388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1605388e9c81SLen Brown 	}
1606ba3dec99SLen Brown 	return 0;
1607103a8feaSLen Brown }
1608c98d5d94SLen Brown 
1609c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
16101b439f01SLen Brown 	      struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2)
1611c98d5d94SLen Brown {
1612ba3dec99SLen Brown 	int retval = 0;
1613ba3dec99SLen Brown 
1614c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1615c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1616c98d5d94SLen Brown 		delta_core(c, c2);
1617c98d5d94SLen Brown 
1618c98d5d94SLen Brown 	/* always calculate thread delta */
1619ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1620ba3dec99SLen Brown 	if (retval)
1621ba3dec99SLen Brown 		return retval;
1622c98d5d94SLen Brown 
1623c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1624c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1625ba3dec99SLen Brown 		retval = delta_package(p, p2);
1626c98d5d94SLen Brown 
1627ba3dec99SLen Brown 	return retval;
1628103a8feaSLen Brown }
1629103a8feaSLen Brown 
1630c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1631103a8feaSLen Brown {
1632388e9c81SLen Brown 	int i;
1633388e9c81SLen Brown 	struct msr_counter *mp;
1634388e9c81SLen Brown 
16353f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
16363f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
16373f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
16383f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
1639d4794f25SYazen Ghannam 	t->tv_delta.tv_sec = 0;
1640d4794f25SYazen Ghannam 	t->tv_delta.tv_usec = 0;
16413f44a5c6SLen Brown 
1642c98d5d94SLen Brown 	t->tsc = 0;
1643c98d5d94SLen Brown 	t->aperf = 0;
1644c98d5d94SLen Brown 	t->mperf = 0;
1645c98d5d94SLen Brown 	t->c1 = 0;
1646103a8feaSLen Brown 
16472af4f9b8SLen Brown 	t->instr_count = 0;
16482af4f9b8SLen Brown 
1649562a2d37SLen Brown 	t->irq_count = 0;
1650562a2d37SLen Brown 	t->smi_count = 0;
1651562a2d37SLen Brown 
1652c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1653c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1654c98d5d94SLen Brown 
1655c98d5d94SLen Brown 	c->c3 = 0;
1656c98d5d94SLen Brown 	c->c6 = 0;
1657c98d5d94SLen Brown 	c->c7 = 0;
16580539ba11SLen Brown 	c->mc6_us = 0;
1659889facbeSLen Brown 	c->core_temp_c = 0;
16609392bd98SCalvin Walton 	c->core_energy = 0;
1661eae97e05SChen Yu 	c->core_throt_cnt = 0;
1662c98d5d94SLen Brown 
16630b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
16640b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
16650b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
16660b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
16670b2bb692SLen Brown 
1668c98d5d94SLen Brown 	p->pc2 = 0;
16690f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1670c98d5d94SLen Brown 		p->pc3 = 0;
16710f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1672c98d5d94SLen Brown 		p->pc6 = 0;
16730f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1674c98d5d94SLen Brown 		p->pc7 = 0;
1675ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1676ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1677ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1678be0e54c4SLen Brown 	p->cpu_lpi = 0;
1679be0e54c4SLen Brown 	p->sys_lpi = 0;
1680889facbeSLen Brown 
1681889facbeSLen Brown 	p->energy_pkg = 0;
1682889facbeSLen Brown 	p->energy_dram = 0;
1683889facbeSLen Brown 	p->energy_cores = 0;
1684889facbeSLen Brown 	p->energy_gfx = 0;
1685889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1686889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1687889facbeSLen Brown 	p->pkg_temp_c = 0;
168827d47356SLen Brown 
1689fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
169027d47356SLen Brown 	p->gfx_mhz = 0;
1691b4b91569SRafael Antognolli 	p->gfx_act_mhz = 0;
1692388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1693388e9c81SLen Brown 		t->counter[i] = 0;
1694388e9c81SLen Brown 
1695388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1696388e9c81SLen Brown 		c->counter[i] = 0;
1697388e9c81SLen Brown 
1698388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1699388e9c81SLen Brown 		p->counter[i] = 0;
1700103a8feaSLen Brown }
17011b439f01SLen Brown 
17021b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1703103a8feaSLen Brown {
1704388e9c81SLen Brown 	int i;
1705388e9c81SLen Brown 	struct msr_counter *mp;
1706388e9c81SLen Brown 
17074c2122d4SLen Brown 	/* copy un-changing apic_id's */
17084c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
17094c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
17104c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
17114c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
17124c2122d4SLen Brown 
17133f44a5c6SLen Brown 	/* remember first tv_begin */
17143f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
17153f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
17163f44a5c6SLen Brown 
17173f44a5c6SLen Brown 	/* remember last tv_end */
17183f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
17193f44a5c6SLen Brown 
1720c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1721c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1722c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1723c98d5d94SLen Brown 	average.threads.c1 += t->c1;
172415aaa346SLen Brown 
17252af4f9b8SLen Brown 	average.threads.instr_count += t->instr_count;
17262af4f9b8SLen Brown 
1727562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1728562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1729562a2d37SLen Brown 
1730388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1731388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1732388e9c81SLen Brown 			continue;
1733388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1734388e9c81SLen Brown 	}
1735388e9c81SLen Brown 
1736c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1737c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1738c98d5d94SLen Brown 		return 0;
1739c98d5d94SLen Brown 
1740c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1741c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1742c98d5d94SLen Brown 	average.cores.c7 += c->c7;
17430539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1744c98d5d94SLen Brown 
1745889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1746eae97e05SChen Yu 	average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt);
1747889facbeSLen Brown 
17489392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
17499392bd98SCalvin Walton 
1750388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1751388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1752388e9c81SLen Brown 			continue;
1753388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1754388e9c81SLen Brown 	}
1755388e9c81SLen Brown 
1756c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1757c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1758c98d5d94SLen Brown 		return 0;
1759c98d5d94SLen Brown 
1760a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
17610b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1762a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
17630b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1764a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
17650b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1766a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
17670b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
17680b2bb692SLen Brown 
1769c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
17700f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1771c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
17720f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1773c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
17740f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1775c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1776ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1777ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1778ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1779c98d5d94SLen Brown 
1780be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1781be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1782be0e54c4SLen Brown 
1783889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1784889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1785889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1786889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1787889facbeSLen Brown 
1788fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
178927d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
1790b4b91569SRafael Antognolli 	average.packages.gfx_act_mhz = p->gfx_act_mhz;
179127d47356SLen Brown 
1792889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1793889facbeSLen Brown 
1794889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1795889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1796388e9c81SLen Brown 
1797388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1798388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1799388e9c81SLen Brown 			continue;
1800388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1801388e9c81SLen Brown 	}
1802c98d5d94SLen Brown 	return 0;
1803c98d5d94SLen Brown }
18041b439f01SLen Brown 
1805c98d5d94SLen Brown /*
1806c98d5d94SLen Brown  * sum the counters for all cpus in the system
1807c98d5d94SLen Brown  * compute the weighted average
1808c98d5d94SLen Brown  */
18091b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1810c98d5d94SLen Brown {
1811388e9c81SLen Brown 	int i;
1812388e9c81SLen Brown 	struct msr_counter *mp;
1813388e9c81SLen Brown 
1814c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1815c98d5d94SLen Brown 
1816c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1817c98d5d94SLen Brown 
1818d4794f25SYazen Ghannam 	/* Use the global time delta for the average. */
1819d4794f25SYazen Ghannam 	average.threads.tv_delta = tv_delta;
1820d4794f25SYazen Ghannam 
1821c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1822c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1823c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
18242af4f9b8SLen Brown 	average.threads.instr_count /= topo.num_cpus;
1825c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1826c98d5d94SLen Brown 
18270de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
18280de6c0dfSLen Brown 		sums_need_wide_columns = 1;
18290de6c0dfSLen Brown 
1830c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1831c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1832c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
18330539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1834c98d5d94SLen Brown 
1835a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
18360b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1837a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
18380b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1839a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
18400b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1841a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
18420b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
18430b2bb692SLen Brown 
1844c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
18450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1846c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
18470f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1848c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
18490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1850c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1851ca58710fSKristen Carlson Accardi 
1852ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1853ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1854ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1855388e9c81SLen Brown 
1856388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1857388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1858388e9c81SLen Brown 			continue;
18590de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18600de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
18610de6c0dfSLen Brown 				sums_need_wide_columns = 1;
186241618e63SLen Brown 			continue;
18630de6c0dfSLen Brown 		}
1864388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1865388e9c81SLen Brown 	}
1866388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1867388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1868388e9c81SLen Brown 			continue;
18690de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18700de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
18710de6c0dfSLen Brown 				sums_need_wide_columns = 1;
18720de6c0dfSLen Brown 		}
1873388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1874388e9c81SLen Brown 	}
1875388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1876388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1877388e9c81SLen Brown 			continue;
18780de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18790de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
18800de6c0dfSLen Brown 				sums_need_wide_columns = 1;
18810de6c0dfSLen Brown 		}
1882388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1883388e9c81SLen Brown 	}
1884c98d5d94SLen Brown }
1885c98d5d94SLen Brown 
1886c98d5d94SLen Brown static unsigned long long rdtsc(void)
1887c98d5d94SLen Brown {
1888c98d5d94SLen Brown 	unsigned int low, high;
1889c98d5d94SLen Brown 
1890c98d5d94SLen Brown 	asm volatile ("rdtsc":"=a" (low), "=d"(high));
1891c98d5d94SLen Brown 
1892c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1893c98d5d94SLen Brown }
1894c98d5d94SLen Brown 
1895c98d5d94SLen Brown /*
1896495c7654SLen Brown  * Open a file, and exit on failure
1897495c7654SLen Brown  */
1898495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1899495c7654SLen Brown {
1900495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1901495c7654SLen Brown 
1902495c7654SLen Brown 	if (!filep)
1903495c7654SLen Brown 		err(1, "%s: open failed", path);
1904495c7654SLen Brown 	return filep;
1905495c7654SLen Brown }
19061b439f01SLen Brown 
1907495c7654SLen Brown /*
1908495c7654SLen Brown  * snapshot_sysfs_counter()
1909495c7654SLen Brown  *
1910495c7654SLen Brown  * return snapshot of given counter
1911495c7654SLen Brown  */
1912495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1913495c7654SLen Brown {
1914495c7654SLen Brown 	FILE *fp;
1915495c7654SLen Brown 	int retval;
1916495c7654SLen Brown 	unsigned long long counter;
1917495c7654SLen Brown 
1918495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1919495c7654SLen Brown 
1920495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1921495c7654SLen Brown 	if (retval != 1)
1922495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1923495c7654SLen Brown 
1924495c7654SLen Brown 	fclose(fp);
1925495c7654SLen Brown 
1926495c7654SLen Brown 	return counter;
1927495c7654SLen Brown }
1928495c7654SLen Brown 
1929495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1930495c7654SLen Brown {
1931495c7654SLen Brown 	if (mp->msr_num != 0) {
1932495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1933495c7654SLen Brown 			return -1;
1934495c7654SLen Brown 	} else {
193546c27978SLen Brown 		char path[128 + PATH_BYTES];
193641618e63SLen Brown 
193741618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
19381b439f01SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path);
193941618e63SLen Brown 
194041618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
194141618e63SLen Brown 		} else {
1942495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1943495c7654SLen Brown 		}
194441618e63SLen Brown 	}
1945495c7654SLen Brown 
1946495c7654SLen Brown 	return 0;
1947495c7654SLen Brown }
1948495c7654SLen Brown 
19496d6501d9SBorislav Petkov int get_epb(int cpu)
19506d6501d9SBorislav Petkov {
19516d6501d9SBorislav Petkov 	char path[128 + PATH_BYTES];
19527f1b11baSBorislav Petkov 	unsigned long long msr;
19536d6501d9SBorislav Petkov 	int ret, epb = -1;
19546d6501d9SBorislav Petkov 	FILE *fp;
19556d6501d9SBorislav Petkov 
19566d6501d9SBorislav Petkov 	sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
19576d6501d9SBorislav Petkov 
19587f1b11baSBorislav Petkov 	fp = fopen(path, "r");
19597f1b11baSBorislav Petkov 	if (!fp)
19607f1b11baSBorislav Petkov 		goto msr_fallback;
19616d6501d9SBorislav Petkov 
19626d6501d9SBorislav Petkov 	ret = fscanf(fp, "%d", &epb);
19636d6501d9SBorislav Petkov 	if (ret != 1)
19646d6501d9SBorislav Petkov 		err(1, "%s(%s)", __func__, path);
19656d6501d9SBorislav Petkov 
19666d6501d9SBorislav Petkov 	fclose(fp);
19676d6501d9SBorislav Petkov 
19686d6501d9SBorislav Petkov 	return epb;
19697f1b11baSBorislav Petkov 
19707f1b11baSBorislav Petkov msr_fallback:
19717f1b11baSBorislav Petkov 	get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
19727f1b11baSBorislav Petkov 
19737f1b11baSBorislav Petkov 	return msr & 0xf;
19746d6501d9SBorislav Petkov }
19756d6501d9SBorislav Petkov 
19764c2122d4SLen Brown void get_apic_id(struct thread_data *t)
19774c2122d4SLen Brown {
197834041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
197934041551SLen Brown 
198034041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
198134041551SLen Brown 		eax = ebx = ecx = edx = 0;
198234041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
198334041551SLen Brown 
198434041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
198534041551SLen Brown 	}
198634041551SLen Brown 
198734041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
198834041551SLen Brown 		return;
198934041551SLen Brown 
1990c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine) {
199134041551SLen Brown 		unsigned int topology_extensions;
199234041551SLen Brown 
199334041551SLen Brown 		if (max_extended_level < 0x8000001e)
199434041551SLen Brown 			return;
19954c2122d4SLen Brown 
19964c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
199734041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
199834041551SLen Brown 		topology_extensions = ecx & (1 << 22);
199934041551SLen Brown 
200034041551SLen Brown 		if (topology_extensions == 0)
200134041551SLen Brown 			return;
200234041551SLen Brown 
200334041551SLen Brown 		eax = ebx = ecx = edx = 0;
200434041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
200534041551SLen Brown 
200634041551SLen Brown 		t->x2apic_id = eax;
200734041551SLen Brown 		return;
200834041551SLen Brown 	}
20094c2122d4SLen Brown 
20104c2122d4SLen Brown 	if (!genuine_intel)
20114c2122d4SLen Brown 		return;
20124c2122d4SLen Brown 
20134c2122d4SLen Brown 	if (max_level < 0xb)
20144c2122d4SLen Brown 		return;
20154c2122d4SLen Brown 
20164c2122d4SLen Brown 	ecx = 0;
20174c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
20184c2122d4SLen Brown 	t->x2apic_id = edx;
20194c2122d4SLen Brown 
202034041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
20211b439f01SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
20224c2122d4SLen Brown }
20234c2122d4SLen Brown 
2024eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt)
2025eae97e05SChen Yu {
2026eae97e05SChen Yu 	char path[128 + PATH_BYTES];
2027eae97e05SChen Yu 	unsigned long long tmp;
2028eae97e05SChen Yu 	FILE *fp;
2029eae97e05SChen Yu 	int ret;
2030eae97e05SChen Yu 
2031eae97e05SChen Yu 	sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu);
2032eae97e05SChen Yu 	fp = fopen(path, "r");
2033eae97e05SChen Yu 	if (!fp)
2034eae97e05SChen Yu 		return -1;
2035eae97e05SChen Yu 	ret = fscanf(fp, "%lld", &tmp);
2036eae97e05SChen Yu 	if (ret != 1)
2037eae97e05SChen Yu 		return -1;
2038eae97e05SChen Yu 	fclose(fp);
2039eae97e05SChen Yu 	*cnt = tmp;
2040eae97e05SChen Yu 
2041eae97e05SChen Yu 	return 0;
2042eae97e05SChen Yu }
2043eae97e05SChen Yu 
2044495c7654SLen Brown /*
2045c98d5d94SLen Brown  * get_counters(...)
2046c98d5d94SLen Brown  * migrate to cpu
2047c98d5d94SLen Brown  * acquire and record local counters for that cpu
2048c98d5d94SLen Brown  */
2049c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2050c98d5d94SLen Brown {
2051c98d5d94SLen Brown 	int cpu = t->cpu_id;
2052889facbeSLen Brown 	unsigned long long msr;
20530102b067SLen Brown 	int aperf_mperf_retry_count = 0;
2054388e9c81SLen Brown 	struct msr_counter *mp;
2055388e9c81SLen Brown 	int i;
2056c98d5d94SLen Brown 
2057e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
20583d7772eaSLen Brown 		fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
205915aaa346SLen Brown 		return -1;
2060e52966c0SLen Brown 	}
206115aaa346SLen Brown 
2062d4794f25SYazen Ghannam 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2063d4794f25SYazen Ghannam 
20644c2122d4SLen Brown 	if (first_counter_read)
20654c2122d4SLen Brown 		get_apic_id(t);
20660102b067SLen Brown retry:
2067c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
206815aaa346SLen Brown 
20691b439f01SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
20700102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
20710102b067SLen Brown 
20720102b067SLen Brown 		/*
20730102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
20740102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
20750102b067SLen Brown 		 *
20760102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
20770102b067SLen Brown 		 * individual system call, so delays may occur
20780102b067SLen Brown 		 * between them.  If the time to read them
20790102b067SLen Brown 		 * varies by a large amount, we re-read them.
20800102b067SLen Brown 		 */
20810102b067SLen Brown 
20820102b067SLen Brown 		/*
20830102b067SLen Brown 		 * This initial dummy APERF read has been seen to
20840102b067SLen Brown 		 * reduce jitter in the subsequent reads.
20850102b067SLen Brown 		 */
20860102b067SLen Brown 
20879c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2088c98d5d94SLen Brown 			return -3;
20890102b067SLen Brown 
20900102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
20910102b067SLen Brown 
20920102b067SLen Brown 		tsc_before = t->tsc;
20930102b067SLen Brown 
20940102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
20950102b067SLen Brown 			return -3;
20960102b067SLen Brown 
20970102b067SLen Brown 		tsc_between = rdtsc();
20980102b067SLen Brown 
20999c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2100c98d5d94SLen Brown 			return -4;
21010102b067SLen Brown 
21020102b067SLen Brown 		tsc_after = rdtsc();
21030102b067SLen Brown 
21040102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
21050102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
21060102b067SLen Brown 
21070102b067SLen Brown 		/*
21080102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
21090102b067SLen Brown 		 * differ by more than 2x, then try again.
21100102b067SLen Brown 		 */
21110102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
21120102b067SLen Brown 			aperf_mperf_retry_count++;
21130102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
21140102b067SLen Brown 				goto retry;
21150102b067SLen Brown 			else
21161b439f01SLen Brown 				warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time);
21170102b067SLen Brown 		}
21180102b067SLen Brown 		aperf_mperf_retry_count = 0;
21190102b067SLen Brown 
2120b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
2121b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
212288c3281fSLen Brown 	}
212388c3281fSLen Brown 
21242af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
21252af4f9b8SLen Brown 		if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
21262af4f9b8SLen Brown 			return -4;
21272af4f9b8SLen Brown 
2128812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
2129562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
2130812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
21311ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
21321ed51011SLen Brown 			return -5;
21331ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
21341ed51011SLen Brown 	}
21350539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2136144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2137144b44b1SLen Brown 			return -6;
2138144b44b1SLen Brown 	}
2139144b44b1SLen Brown 
2140388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2141495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
2142388e9c81SLen Brown 			return -10;
2143388e9c81SLen Brown 	}
2144388e9c81SLen Brown 
2145c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
2146c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2147f4fdf2b4SLen Brown 		goto done;
2148c98d5d94SLen Brown 
21491e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2150c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2151c98d5d94SLen Brown 			return -6;
2152144b44b1SLen Brown 	}
2153144b44b1SLen Brown 
21541e9042b9SSrinivas Pandruvada 	if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2155c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2156c98d5d94SLen Brown 			return -7;
21571e9042b9SSrinivas Pandruvada 	} else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
2158fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2159fb5d4327SDasaratharaman Chandramouli 			return -7;
2160103a8feaSLen Brown 	}
216115aaa346SLen Brown 
21627ab5ff49SZhang Rui 	if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2163c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2164c98d5d94SLen Brown 			return -8;
21657ab5ff49SZhang Rui 		else if (t->is_atom) {
21667ab5ff49SZhang Rui 			/*
21677ab5ff49SZhang Rui 			 * For Atom CPUs that has core cstate deeper than c6,
21687ab5ff49SZhang Rui 			 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
21697ab5ff49SZhang Rui 			 * Minus CC7 (and deeper cstates) residency to get
21707ab5ff49SZhang Rui 			 * accturate cc6 residency.
21717ab5ff49SZhang Rui 			 */
21727ab5ff49SZhang Rui 			c->c6 -= c->c7;
21737ab5ff49SZhang Rui 		}
21747ab5ff49SZhang Rui 	}
2175c98d5d94SLen Brown 
21760539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
21770539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
21780539ba11SLen Brown 			return -8;
21790539ba11SLen Brown 
2180812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
2181889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2182889facbeSLen Brown 			return -9;
218355279aefSZhang Rui 		c->core_temp_c = tj_max - ((msr >> 16) & 0x7F);
2184889facbeSLen Brown 	}
2185889facbeSLen Brown 
2186eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
2187eae97e05SChen Yu 		get_core_throt_cnt(cpu, &c->core_throt_cnt);
2188eae97e05SChen Yu 
21899392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
21909392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
21919392bd98SCalvin Walton 			return -14;
21929392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
21939392bd98SCalvin Walton 	}
21949392bd98SCalvin Walton 
2195388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2196495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
2197388e9c81SLen Brown 			return -10;
2198388e9c81SLen Brown 	}
2199889facbeSLen Brown 
2200c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
2201c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2202f4fdf2b4SLen Brown 		goto done;
220315aaa346SLen Brown 
2204a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
22050b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
22060b2bb692SLen Brown 			return -10;
2207a99d8730SLen Brown 	}
2208a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
22090b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
22100b2bb692SLen Brown 			return -11;
2211a99d8730SLen Brown 	}
2212a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
22130b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
22140b2bb692SLen Brown 			return -12;
2215a99d8730SLen Brown 	}
2216a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
22170b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
22180b2bb692SLen Brown 			return -13;
22190b2bb692SLen Brown 	}
22200f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
2221c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2222c98d5d94SLen Brown 			return -9;
22230f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
22240539ba11SLen Brown 		if (do_slm_cstates) {
22250539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
22260539ba11SLen Brown 				return -10;
22270539ba11SLen Brown 		} else {
2228c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2229c98d5d94SLen Brown 				return -10;
22300539ba11SLen Brown 		}
22310539ba11SLen Brown 	}
22320539ba11SLen Brown 
22330f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
2234c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2235c98d5d94SLen Brown 			return -11;
22360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
2237c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2238c98d5d94SLen Brown 			return -12;
22390f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
2240ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2241ca58710fSKristen Carlson Accardi 			return -13;
22420f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
2243ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2244ca58710fSKristen Carlson Accardi 			return -13;
22450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
2246ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2247ca58710fSKristen Carlson Accardi 			return -13;
22480f47c08dSLen Brown 
2249be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2250be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2251be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2252be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
2253be0e54c4SLen Brown 
2254889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
22559972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2256889facbeSLen Brown 			return -13;
22579972d5d8SChen Yu 		p->energy_pkg = msr;
2258889facbeSLen Brown 	}
22599148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
22609972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2261889facbeSLen Brown 			return -14;
22629972d5d8SChen Yu 		p->energy_cores = msr;
2263889facbeSLen Brown 	}
2264889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
22659972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2266889facbeSLen Brown 			return -15;
22679972d5d8SChen Yu 		p->energy_dram = msr;
2268889facbeSLen Brown 	}
2269889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
22709972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2271889facbeSLen Brown 			return -16;
22729972d5d8SChen Yu 		p->energy_gfx = msr;
2273889facbeSLen Brown 	}
2274889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
22759972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2276889facbeSLen Brown 			return -16;
22779972d5d8SChen Yu 		p->rapl_pkg_perf_status = msr;
2278889facbeSLen Brown 	}
2279889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
22809972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2281889facbeSLen Brown 			return -16;
22829972d5d8SChen Yu 		p->rapl_dram_perf_status = msr;
2283889facbeSLen Brown 	}
22843316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
22859972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
22863316f99aSCalvin Walton 			return -13;
22879972d5d8SChen Yu 		p->energy_pkg = msr;
22883316f99aSCalvin Walton 	}
2289812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
2290889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2291889facbeSLen Brown 			return -17;
229255279aefSZhang Rui 		p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F);
2293889facbeSLen Brown 	}
2294fdf676e5SLen Brown 
2295812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2296fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
2297fdf676e5SLen Brown 
2298812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
229927d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
230027d47356SLen Brown 
2301b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
2302b4b91569SRafael Antognolli 		p->gfx_act_mhz = gfx_act_mhz;
2303b4b91569SRafael Antognolli 
2304388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2305495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
2306388e9c81SLen Brown 			return -10;
2307388e9c81SLen Brown 	}
2308f4fdf2b4SLen Brown done:
2309f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
2310388e9c81SLen Brown 
231115aaa346SLen Brown 	return 0;
2312103a8feaSLen Brown }
2313103a8feaSLen Brown 
2314ee7e38e3SLen Brown /*
2315ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2316ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2317ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2318ee7e38e3SLen Brown  */
2319ee7e38e3SLen Brown 
2320ee7e38e3SLen Brown #define PCLUKN 0		/* Unknown */
2321ee7e38e3SLen Brown #define PCLRSV 1		/* Reserved */
2322ee7e38e3SLen Brown #define PCL__0 2		/* PC0 */
2323ee7e38e3SLen Brown #define PCL__1 3		/* PC1 */
2324ee7e38e3SLen Brown #define PCL__2 4		/* PC2 */
2325ee7e38e3SLen Brown #define PCL__3 5		/* PC3 */
2326ee7e38e3SLen Brown #define PCL__4 6		/* PC4 */
2327ee7e38e3SLen Brown #define PCL__6 7		/* PC6 */
2328ee7e38e3SLen Brown #define PCL_6N 8		/* PC6 No Retention */
2329ee7e38e3SLen Brown #define PCL_6R 9		/* PC6 Retention */
2330ee7e38e3SLen Brown #define PCL__7 10		/* PC7 */
2331ee7e38e3SLen Brown #define PCL_7S 11		/* PC7 Shrink */
23320b2bb692SLen Brown #define PCL__8 12		/* PC8 */
23330b2bb692SLen Brown #define PCL__9 13		/* PC9 */
2334445640a5SLen Brown #define PCL_10 14		/* PC10 */
2335445640a5SLen Brown #define PCLUNL 15		/* Unlimited */
2336ee7e38e3SLen Brown 
2337ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2338ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
23391b439f01SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"
23401b439f01SLen Brown };
2341ee7e38e3SLen Brown 
23421b439f01SLen Brown int nhm_pkg_cstate_limits[16] =
23431b439f01SLen Brown     { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
234438c6663aSLen Brown 	PCLRSV, PCLRSV
234538c6663aSLen Brown };
234638c6663aSLen Brown 
23471b439f01SLen Brown int snb_pkg_cstate_limits[16] =
23481b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
234938c6663aSLen Brown 	PCLRSV, PCLRSV
235038c6663aSLen Brown };
235138c6663aSLen Brown 
23521b439f01SLen Brown int hsw_pkg_cstate_limits[16] =
23531b439f01SLen Brown     { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
235438c6663aSLen Brown 	PCLRSV, PCLRSV
235538c6663aSLen Brown };
235638c6663aSLen Brown 
23571b439f01SLen Brown int slv_pkg_cstate_limits[16] =
23581b439f01SLen Brown     { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
235938c6663aSLen Brown 	PCL__6, PCL__7
236038c6663aSLen Brown };
236138c6663aSLen Brown 
23621b439f01SLen Brown int amt_pkg_cstate_limits[16] =
23631b439f01SLen Brown     { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
236438c6663aSLen Brown 	PCLRSV, PCLRSV
236538c6663aSLen Brown };
236638c6663aSLen Brown 
23671b439f01SLen Brown int phi_pkg_cstate_limits[16] =
23681b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
236938c6663aSLen Brown 	PCLRSV, PCLRSV
237038c6663aSLen Brown };
237138c6663aSLen Brown 
23721b439f01SLen Brown int glm_pkg_cstate_limits[16] =
23731b439f01SLen Brown     { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
237438c6663aSLen Brown 	PCLRSV, PCLRSV
237538c6663aSLen Brown };
237638c6663aSLen Brown 
23771b439f01SLen Brown int skx_pkg_cstate_limits[16] =
23781b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
237938c6663aSLen Brown 	PCLRSV, PCLRSV
238038c6663aSLen Brown };
238138c6663aSLen Brown 
23821b439f01SLen Brown int icx_pkg_cstate_limits[16] =
23831b439f01SLen Brown     { PCL__0, PCL__2, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
238438c6663aSLen Brown 	PCLRSV, PCLRSV
238538c6663aSLen Brown };
2386a2b7b749SLen Brown 
23871b439f01SLen Brown static void calculate_tsc_tweak()
2388a2b7b749SLen Brown {
2389a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2390a2b7b749SLen Brown }
2391a2b7b749SLen Brown 
2392aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model);
2393aeb01e6dSChen Yu 
23941b439f01SLen Brown static void dump_nhm_platform_info(void)
2395103a8feaSLen Brown {
2396103a8feaSLen Brown 	unsigned long long msr;
2397103a8feaSLen Brown 	unsigned int ratio;
2398103a8feaSLen Brown 
2399ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2400103a8feaSLen Brown 
2401b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
24026574a5d5SLen Brown 
2403103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
24041b439f01SLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk);
2405103a8feaSLen Brown 
2406103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
24071b439f01SLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2408103a8feaSLen Brown 
24097ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2410b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2411bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
241267920418SLen Brown 
2413aeb01e6dSChen Yu 	/* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2414aeb01e6dSChen Yu 	if (dis_cstate_prewake)
24151b439f01SLen Brown 		fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN");
2416aeb01e6dSChen Yu 
2417fcd17211SLen Brown 	return;
2418fcd17211SLen Brown }
2419fcd17211SLen Brown 
24201b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void)
2421fcd17211SLen Brown {
2422fcd17211SLen Brown 	unsigned long long msr;
2423fcd17211SLen Brown 	unsigned int ratio;
2424fcd17211SLen Brown 
24257ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2426fcd17211SLen Brown 
2427b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2428fcd17211SLen Brown 
2429fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2430fcd17211SLen Brown 	if (ratio)
24311b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk);
2432fcd17211SLen Brown 
2433fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2434fcd17211SLen Brown 	if (ratio)
24351b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk);
2436fcd17211SLen Brown 	return;
2437fcd17211SLen Brown }
2438fcd17211SLen Brown 
24391b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void)
2440fcd17211SLen Brown {
2441fcd17211SLen Brown 	unsigned long long msr;
2442fcd17211SLen Brown 	unsigned int ratio;
24436574a5d5SLen Brown 
24447ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
24456574a5d5SLen Brown 
2446b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
24476574a5d5SLen Brown 
24486574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
24496574a5d5SLen Brown 	if (ratio)
24501b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk);
24516574a5d5SLen Brown 
24526574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
24536574a5d5SLen Brown 	if (ratio)
24541b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk);
24556574a5d5SLen Brown 
24566574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
24576574a5d5SLen Brown 	if (ratio)
24581b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk);
24596574a5d5SLen Brown 
24606574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
24616574a5d5SLen Brown 	if (ratio)
24621b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk);
24636574a5d5SLen Brown 
24646574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
24656574a5d5SLen Brown 	if (ratio)
24661b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk);
24676574a5d5SLen Brown 
24686574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
24696574a5d5SLen Brown 	if (ratio)
24701b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk);
24716574a5d5SLen Brown 
24726574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
24736574a5d5SLen Brown 	if (ratio)
24741b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk);
24756574a5d5SLen Brown 
24766574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
24776574a5d5SLen Brown 	if (ratio)
24781b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk);
2479103a8feaSLen Brown 	return;
2480fcd17211SLen Brown }
24811b439f01SLen Brown 
248231e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
248331e07522SLen Brown {
248431e07522SLen Brown 
248531e07522SLen Brown 	if (!genuine_intel)
248631e07522SLen Brown 		return 0;
248731e07522SLen Brown 
248831e07522SLen Brown 	switch (model) {
248931e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
249031e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
249125368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
24925ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
249320de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:
249431e07522SLen Brown 		return 1;
249531e07522SLen Brown 	}
249631e07522SLen Brown 	return 0;
249731e07522SLen Brown }
2498fcd17211SLen Brown 
24991b439f01SLen Brown static void dump_turbo_ratio_limits(int family, int model)
2500fcd17211SLen Brown {
250131e07522SLen Brown 	unsigned long long msr, core_counts;
250231e07522SLen Brown 	unsigned int ratio, group_size;
2503103a8feaSLen Brown 
25047ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2505b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
25066574a5d5SLen Brown 
250731e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
250831e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
250931e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
251031e07522SLen Brown 	} else {
251131e07522SLen Brown 		core_counts = 0x0807060504030201;
251231e07522SLen Brown 	}
251331e07522SLen Brown 
25146574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
251531e07522SLen Brown 	group_size = (core_counts >> 56) & 0xFF;
25166574a5d5SLen Brown 	if (ratio)
251731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
251831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
25196574a5d5SLen Brown 
25206574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
252131e07522SLen Brown 	group_size = (core_counts >> 48) & 0xFF;
25226574a5d5SLen Brown 	if (ratio)
252331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
252431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
25256574a5d5SLen Brown 
25266574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
252731e07522SLen Brown 	group_size = (core_counts >> 40) & 0xFF;
25286574a5d5SLen Brown 	if (ratio)
252931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
253031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
25316574a5d5SLen Brown 
25326574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
253331e07522SLen Brown 	group_size = (core_counts >> 32) & 0xFF;
25346574a5d5SLen Brown 	if (ratio)
253531e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
253631e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
25376574a5d5SLen Brown 
2538103a8feaSLen Brown 	ratio = (msr >> 24) & 0xFF;
253931e07522SLen Brown 	group_size = (core_counts >> 24) & 0xFF;
2540103a8feaSLen Brown 	if (ratio)
254131e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
254231e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2543103a8feaSLen Brown 
2544103a8feaSLen Brown 	ratio = (msr >> 16) & 0xFF;
254531e07522SLen Brown 	group_size = (core_counts >> 16) & 0xFF;
2546103a8feaSLen Brown 	if (ratio)
254731e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
254831e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2549103a8feaSLen Brown 
2550103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
255131e07522SLen Brown 	group_size = (core_counts >> 8) & 0xFF;
2552103a8feaSLen Brown 	if (ratio)
255331e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
255431e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2555103a8feaSLen Brown 
2556103a8feaSLen Brown 	ratio = (msr >> 0) & 0xFF;
255731e07522SLen Brown 	group_size = (core_counts >> 0) & 0xFF;
2558103a8feaSLen Brown 	if (ratio)
255931e07522SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
256031e07522SLen Brown 			ratio, bclk, ratio * bclk, group_size);
2561fcd17211SLen Brown 	return;
2562fcd17211SLen Brown }
25633a9a941dSLen Brown 
25641b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void)
25650f7887c4SLen Brown {
25660f7887c4SLen Brown 	unsigned long long msr;
25670f7887c4SLen Brown 	unsigned int ratio;
25680f7887c4SLen Brown 
25690f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
25700f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25710f7887c4SLen Brown 
25720f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
25730f7887c4SLen Brown 	if (ratio)
25741b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk);
25750f7887c4SLen Brown 
25760f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
25770f7887c4SLen Brown 	if (ratio)
25781b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk);
25790f7887c4SLen Brown 
25800f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
25810f7887c4SLen Brown 	if (ratio)
25821b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
25830f7887c4SLen Brown 
25840f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
25850f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25860f7887c4SLen Brown 
25870f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
25880f7887c4SLen Brown 	if (ratio)
25891b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk);
25900f7887c4SLen Brown 
25910f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
25920f7887c4SLen Brown 	if (ratio)
25931b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk);
25940f7887c4SLen Brown 
25950f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
25960f7887c4SLen Brown 	if (ratio)
25971b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk);
25980f7887c4SLen Brown 
25990f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
26000f7887c4SLen Brown 	if (ratio)
26011b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk);
26020f7887c4SLen Brown }
26030f7887c4SLen Brown 
26041b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void)
2605fb5d4327SDasaratharaman Chandramouli {
2606cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2607cbf97abaSHubert Chrzaniuk 
2608fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2609cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2610cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2611cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2612cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2613fb5d4327SDasaratharaman Chandramouli 
2614ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2615fb5d4327SDasaratharaman Chandramouli 
26161b439f01SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2617fb5d4327SDasaratharaman Chandramouli 
26181e3ec5cdSRandy Dunlap 	/*
2619fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2620cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2621cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2622fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2623fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2624fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2625fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2626fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2627fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2628fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2629fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2630fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2631fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2632fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2633fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2634fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2635fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2636fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2637fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2638fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2639fb5d4327SDasaratharaman Chandramouli 	 */
2640fb5d4327SDasaratharaman Chandramouli 
2641cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2642cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2643cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2644cbf97abaSHubert Chrzaniuk 
2645cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2646fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2647cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2648fb5d4327SDasaratharaman Chandramouli 
2649cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2650cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2651cbf97abaSHubert Chrzaniuk 		b_nr++;
2652fb5d4327SDasaratharaman Chandramouli 	}
2653cbf97abaSHubert Chrzaniuk 
2654cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2655cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2656b7d8c148SLen Brown 			fprintf(outf,
2657710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2658cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2659fb5d4327SDasaratharaman Chandramouli }
2660fb5d4327SDasaratharaman Chandramouli 
26611b439f01SLen Brown static void dump_nhm_cst_cfg(void)
2662fcd17211SLen Brown {
2663fcd17211SLen Brown 	unsigned long long msr;
2664fcd17211SLen Brown 
26651df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2666fcd17211SLen Brown 
26671df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2668fcd17211SLen Brown 
26693e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2670fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2671fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2672fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2673fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
26741b439f01SLen Brown 		(msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]);
2675ac980e13SArtem Bityutskiy 
2676ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2677ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
26781b439f01SLen Brown 		fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2679ac980e13SArtem Bityutskiy 	}
2680ac980e13SArtem Bityutskiy 
2681ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2682ac980e13SArtem Bityutskiy 
2683fcd17211SLen Brown 	return;
2684103a8feaSLen Brown }
2685103a8feaSLen Brown 
26861b439f01SLen Brown static void dump_config_tdp(void)
26876fb3143bSLen Brown {
26886fb3143bSLen Brown 	unsigned long long msr;
26896fb3143bSLen Brown 
26906fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2691b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2692685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
26936fb3143bSLen Brown 
26946fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2695b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
26966fb3143bSLen Brown 	if (msr) {
2697685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2698685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2699685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2700685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
27016fb3143bSLen Brown 	}
2702b7d8c148SLen Brown 	fprintf(outf, ")\n");
27036fb3143bSLen Brown 
27046fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2705b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
27066fb3143bSLen Brown 	if (msr) {
2707685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2708685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2709685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2710685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
27116fb3143bSLen Brown 	}
2712b7d8c148SLen Brown 	fprintf(outf, ")\n");
27136fb3143bSLen Brown 
27146fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2715b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
27166fb3143bSLen Brown 	if ((msr) & 0x3)
2717b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2718b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2719b7d8c148SLen Brown 	fprintf(outf, ")\n");
27206fb3143bSLen Brown 
27216fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2722b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2723685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2724b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2725b7d8c148SLen Brown 	fprintf(outf, ")\n");
27266fb3143bSLen Brown }
27275a63426eSLen Brown 
27285a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
27295a63426eSLen Brown 
27305a63426eSLen Brown void print_irtl(void)
27315a63426eSLen Brown {
27325a63426eSLen Brown 	unsigned long long msr;
27335a63426eSLen Brown 
27345a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
27355a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
27365a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27375a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27385a63426eSLen Brown 
27395a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
27405a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
27415a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27425a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27435a63426eSLen Brown 
27445a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
27455a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
27465a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27475a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27485a63426eSLen Brown 
27495a63426eSLen Brown 	if (!do_irtl_hsw)
27505a63426eSLen Brown 		return;
27515a63426eSLen Brown 
27525a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
27535a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
27545a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27555a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27565a63426eSLen Brown 
27575a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
27585a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
27595a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27605a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27615a63426eSLen Brown 
27625a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
27635a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
27645a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27655a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27665a63426eSLen Brown 
27675a63426eSLen Brown }
27681b439f01SLen Brown 
276936229897SLen Brown void free_fd_percpu(void)
277036229897SLen Brown {
277136229897SLen Brown 	int i;
277236229897SLen Brown 
277301a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
277436229897SLen Brown 		if (fd_percpu[i] != 0)
277536229897SLen Brown 			close(fd_percpu[i]);
277636229897SLen Brown 	}
277736229897SLen Brown 
277836229897SLen Brown 	free(fd_percpu);
27796fb3143bSLen Brown }
27806fb3143bSLen Brown 
2781c98d5d94SLen Brown void free_all_buffers(void)
2782103a8feaSLen Brown {
27830e2d8f05SLen Brown 	int i;
27840e2d8f05SLen Brown 
2785c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2786c98d5d94SLen Brown 	cpu_present_set = NULL;
278736229897SLen Brown 	cpu_present_setsize = 0;
2788103a8feaSLen Brown 
2789c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2790c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2791c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2792103a8feaSLen Brown 
2793c98d5d94SLen Brown 	free(thread_even);
2794c98d5d94SLen Brown 	free(core_even);
2795c98d5d94SLen Brown 	free(package_even);
2796103a8feaSLen Brown 
2797c98d5d94SLen Brown 	thread_even = NULL;
2798c98d5d94SLen Brown 	core_even = NULL;
2799c98d5d94SLen Brown 	package_even = NULL;
2800103a8feaSLen Brown 
2801c98d5d94SLen Brown 	free(thread_odd);
2802c98d5d94SLen Brown 	free(core_odd);
2803c98d5d94SLen Brown 	free(package_odd);
2804103a8feaSLen Brown 
2805c98d5d94SLen Brown 	thread_odd = NULL;
2806c98d5d94SLen Brown 	core_odd = NULL;
2807c98d5d94SLen Brown 	package_odd = NULL;
2808103a8feaSLen Brown 
2809c98d5d94SLen Brown 	free(output_buffer);
2810c98d5d94SLen Brown 	output_buffer = NULL;
2811c98d5d94SLen Brown 	outp = NULL;
281236229897SLen Brown 
281336229897SLen Brown 	free_fd_percpu();
2814562a2d37SLen Brown 
2815562a2d37SLen Brown 	free(irq_column_2_cpu);
2816562a2d37SLen Brown 	free(irqs_per_cpu);
28170e2d8f05SLen Brown 
28180e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
28190e2d8f05SLen Brown 		if (cpus[i].put_ids)
28200e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
28210e2d8f05SLen Brown 	}
28220e2d8f05SLen Brown 	free(cpus);
2823103a8feaSLen Brown }
2824103a8feaSLen Brown 
282557a42a34SJosh Triplett /*
282695aebc44SJosh Triplett  * Parse a file containing a single int.
28276de68fe1SLen Brown  * Return 0 if file can not be opened
28286de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
282995aebc44SJosh Triplett  */
283095aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
283195aebc44SJosh Triplett {
283295aebc44SJosh Triplett 	va_list args;
283395aebc44SJosh Triplett 	char path[PATH_MAX];
283495aebc44SJosh Triplett 	FILE *filep;
283595aebc44SJosh Triplett 	int value;
283695aebc44SJosh Triplett 
283795aebc44SJosh Triplett 	va_start(args, fmt);
283895aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
283995aebc44SJosh Triplett 	va_end(args);
28406de68fe1SLen Brown 	filep = fopen(path, "r");
28416de68fe1SLen Brown 	if (!filep)
28426de68fe1SLen Brown 		return 0;
2843b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2844b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
284595aebc44SJosh Triplett 	fclose(filep);
284695aebc44SJosh Triplett 	return value;
284795aebc44SJosh Triplett }
284895aebc44SJosh Triplett 
284995aebc44SJosh Triplett /*
2850c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2851c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2852c98d5d94SLen Brown  */
2853c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2854c98d5d94SLen Brown {
285595aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2856103a8feaSLen Brown }
2857103a8feaSLen Brown 
2858103a8feaSLen Brown int get_physical_package_id(int cpu)
2859103a8feaSLen Brown {
286095aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2861103a8feaSLen Brown }
2862103a8feaSLen Brown 
28636de68fe1SLen Brown int get_die_id(int cpu)
28646de68fe1SLen Brown {
28656de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
28666de68fe1SLen Brown }
28676de68fe1SLen Brown 
2868103a8feaSLen Brown int get_core_id(int cpu)
2869103a8feaSLen Brown {
287095aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2871103a8feaSLen Brown }
2872103a8feaSLen Brown 
2873ef605741SPrarit Bhargava void set_node_data(void)
2874ef605741SPrarit Bhargava {
28752ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
28762ffbb224SPrarit Bhargava 	int cpu_count;
2877ef605741SPrarit Bhargava 
28782ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
28792ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
28802ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2881ef605741SPrarit Bhargava 
28822ffbb224SPrarit Bhargava 	cpu_count = 0;
28832ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
28842ffbb224SPrarit Bhargava 		lnode = 0;
28852ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
28862ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2887ef605741SPrarit Bhargava 				continue;
28882ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
28892ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
28902ffbb224SPrarit Bhargava 				continue;
28912ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2892ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
28932ffbb224SPrarit Bhargava 			cpu_count++;
28942ffbb224SPrarit Bhargava 			/*
28952ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
28962ffbb224SPrarit Bhargava 			 * the logical_node_id
28972ffbb224SPrarit Bhargava 			 */
28982ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
28991b439f01SLen Brown 				if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) {
29002ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
29012ffbb224SPrarit Bhargava 					cpu_count++;
2902ef605741SPrarit Bhargava 				}
29032ffbb224SPrarit Bhargava 			}
29042ffbb224SPrarit Bhargava 			lnode++;
29052ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
29062ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
29072ffbb224SPrarit Bhargava 		}
29082ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
29092ffbb224SPrarit Bhargava 			break;
29102ffbb224SPrarit Bhargava 	}
2911ef605741SPrarit Bhargava }
2912ef605741SPrarit Bhargava 
2913ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2914c98d5d94SLen Brown {
2915c98d5d94SLen Brown 	char path[80];
2916c98d5d94SLen Brown 	FILE *filep;
29170e2d8f05SLen Brown 	int i;
29180e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2919c98d5d94SLen Brown 
29200e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
29211b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i);
29220e2d8f05SLen Brown 		filep = fopen(path, "r");
29230e2d8f05SLen Brown 		if (!filep)
29240e2d8f05SLen Brown 			continue;
29250e2d8f05SLen Brown 		fclose(filep);
29260e2d8f05SLen Brown 		return i;
29270e2d8f05SLen Brown 	}
29280e2d8f05SLen Brown 	return -1;
2929e275b388SDasaratharaman Chandramouli }
2930c98d5d94SLen Brown 
29310e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
29320e2d8f05SLen Brown {
29330e2d8f05SLen Brown 	char path[80], character;
29340e2d8f05SLen Brown 	FILE *filep;
29350e2d8f05SLen Brown 	unsigned long map;
29368cb48b32SPrarit Bhargava 	int so, shift, sib_core;
29370e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
29380e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
29390e2d8f05SLen Brown 	size_t size;
29408cb48b32SPrarit Bhargava 	int thread_id = 0;
29410e2d8f05SLen Brown 
29420e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
29438cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
29448cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
29450e2d8f05SLen Brown 	if (!thiscpu->put_ids)
29460e2d8f05SLen Brown 		return -1;
29470e2d8f05SLen Brown 
29480e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
29490e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
29500e2d8f05SLen Brown 
29511b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
29523d7772eaSLen Brown 	filep = fopen(path, "r");
29533d7772eaSLen Brown 
29543d7772eaSLen Brown 	if (!filep) {
29553d7772eaSLen Brown 		warnx("%s: open failed", path);
29563d7772eaSLen Brown 		return -1;
29573d7772eaSLen Brown 	}
29580e2d8f05SLen Brown 	do {
29590e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
29608173c336SBen Hutchings 		if (fscanf(filep, "%lx%c", &map, &character) != 2)
29618173c336SBen Hutchings 			err(1, "%s: failed to parse file", path);
29620e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
29630e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
29648cb48b32SPrarit Bhargava 				so = shift + offset;
29658cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
29668cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
29678cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
29681b439f01SLen Brown 					if ((so != cpu) && (cpus[so].thread_id < 0))
29691b439f01SLen Brown 						cpus[so].thread_id = thread_id++;
29708cb48b32SPrarit Bhargava 				}
29710e2d8f05SLen Brown 			}
29720e2d8f05SLen Brown 		}
29730e2d8f05SLen Brown 	} while (!strncmp(&character, ",", 1));
2974c98d5d94SLen Brown 	fclose(filep);
29750e2d8f05SLen Brown 
29760e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2977c98d5d94SLen Brown }
2978c98d5d94SLen Brown 
2979c98d5d94SLen Brown /*
2980c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2981c98d5d94SLen Brown  * skip non-present cpus
2982103a8feaSLen Brown  */
2983103a8feaSLen Brown 
2984c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *,
2985c98d5d94SLen Brown 			       struct pkg_data *, struct thread_data *, struct core_data *,
2986c98d5d94SLen Brown 			       struct pkg_data *), struct thread_data *thread_base,
2987c98d5d94SLen Brown 		   struct core_data *core_base, struct pkg_data *pkg_base,
29881b439f01SLen Brown 		   struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2)
2989c98d5d94SLen Brown {
299040f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2991c98d5d94SLen Brown 
2992c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
299340f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
29941b439f01SLen Brown 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
29951b439f01SLen Brown 				for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
2996c98d5d94SLen Brown 					struct thread_data *t, *t2;
2997c98d5d94SLen Brown 					struct core_data *c, *c2;
2998c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2999c98d5d94SLen Brown 
30001b439f01SLen Brown 					t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
3001c98d5d94SLen Brown 
3002c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
3003c98d5d94SLen Brown 						continue;
3004c98d5d94SLen Brown 
30051b439f01SLen Brown 					t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no);
3006c98d5d94SLen Brown 
30071b439f01SLen Brown 					c = GET_CORE(core_base, core_no, node_no, pkg_no);
30081b439f01SLen Brown 					c2 = GET_CORE(core_base2, core_no, node_no, pkg_no);
3009c98d5d94SLen Brown 
3010c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
3011c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
3012c98d5d94SLen Brown 
3013c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
3014c98d5d94SLen Brown 					if (retval)
3015c98d5d94SLen Brown 						return retval;
3016c98d5d94SLen Brown 				}
3017c98d5d94SLen Brown 			}
3018c98d5d94SLen Brown 		}
301940f5cfe7SPrarit Bhargava 	}
3020c98d5d94SLen Brown 	return 0;
3021c98d5d94SLen Brown }
3022c98d5d94SLen Brown 
3023c98d5d94SLen Brown /*
3024c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
3025c98d5d94SLen Brown  * return max_cpu number
3026c98d5d94SLen Brown  */
3027c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int))
3028103a8feaSLen Brown {
3029103a8feaSLen Brown 	FILE *fp;
3030c98d5d94SLen Brown 	int cpu_num;
3031103a8feaSLen Brown 	int retval;
3032103a8feaSLen Brown 
303357a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
3034103a8feaSLen Brown 
3035103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
3036b2c95d90SJosh Triplett 	if (retval != 0)
3037b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
3038103a8feaSLen Brown 
3039c98d5d94SLen Brown 	while (1) {
3040c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
3041103a8feaSLen Brown 		if (retval != 1)
3042103a8feaSLen Brown 			break;
3043103a8feaSLen Brown 
3044c98d5d94SLen Brown 		retval = func(cpu_num);
3045c98d5d94SLen Brown 		if (retval) {
3046c98d5d94SLen Brown 			fclose(fp);
3047c98d5d94SLen Brown 			return (retval);
3048c98d5d94SLen Brown 		}
3049103a8feaSLen Brown 	}
3050103a8feaSLen Brown 	fclose(fp);
3051c98d5d94SLen Brown 	return 0;
3052103a8feaSLen Brown }
3053103a8feaSLen Brown 
3054103a8feaSLen Brown void re_initialize(void)
3055103a8feaSLen Brown {
3056c98d5d94SLen Brown 	free_all_buffers();
3057c98d5d94SLen Brown 	setup_all_buffers();
30583d7772eaSLen Brown 	fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3059103a8feaSLen Brown }
3060103a8feaSLen Brown 
3061843c5791SPrarit Bhargava void set_max_cpu_num(void)
3062843c5791SPrarit Bhargava {
3063843c5791SPrarit Bhargava 	FILE *filep;
30648201a028SPrarit Bhargava 	int base_cpu;
3065843c5791SPrarit Bhargava 	unsigned long dummy;
30668201a028SPrarit Bhargava 	char pathname[64];
3067843c5791SPrarit Bhargava 
30688201a028SPrarit Bhargava 	base_cpu = sched_getcpu();
30698201a028SPrarit Bhargava 	if (base_cpu < 0)
30708201a028SPrarit Bhargava 		err(1, "cannot find calling cpu ID");
30711b439f01SLen Brown 	sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu);
30728201a028SPrarit Bhargava 
30738201a028SPrarit Bhargava 	filep = fopen_or_die(pathname, "r");
3074843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
3075843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
30760e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
3077843c5791SPrarit Bhargava 	fclose(filep);
3078843c5791SPrarit Bhargava 	topo.max_cpu_num--;	/* 0 based */
3079843c5791SPrarit Bhargava }
3080c98d5d94SLen Brown 
3081103a8feaSLen Brown /*
3082c98d5d94SLen Brown  * count_cpus()
3083c98d5d94SLen Brown  * remember the last one seen, it will be the max
3084103a8feaSLen Brown  */
3085c98d5d94SLen Brown int count_cpus(int cpu)
3086103a8feaSLen Brown {
3087843c5791SPrarit Bhargava 	topo.num_cpus++;
3088c98d5d94SLen Brown 	return 0;
3089103a8feaSLen Brown }
30901b439f01SLen Brown 
3091c98d5d94SLen Brown int mark_cpu_present(int cpu)
3092c98d5d94SLen Brown {
3093c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_present_setsize, cpu_present_set);
309415aaa346SLen Brown 	return 0;
3095103a8feaSLen Brown }
3096103a8feaSLen Brown 
30978cb48b32SPrarit Bhargava int init_thread_id(int cpu)
30988cb48b32SPrarit Bhargava {
30998cb48b32SPrarit Bhargava 	cpus[cpu].thread_id = -1;
31008cb48b32SPrarit Bhargava 	return 0;
31018cb48b32SPrarit Bhargava }
31028cb48b32SPrarit Bhargava 
3103562a2d37SLen Brown /*
3104562a2d37SLen Brown  * snapshot_proc_interrupts()
3105562a2d37SLen Brown  *
3106562a2d37SLen Brown  * read and record summary of /proc/interrupts
3107562a2d37SLen Brown  *
3108562a2d37SLen Brown  * return 1 if config change requires a restart, else return 0
3109562a2d37SLen Brown  */
3110562a2d37SLen Brown int snapshot_proc_interrupts(void)
3111562a2d37SLen Brown {
3112562a2d37SLen Brown 	static FILE *fp;
3113562a2d37SLen Brown 	int column, retval;
3114562a2d37SLen Brown 
3115562a2d37SLen Brown 	if (fp == NULL)
3116562a2d37SLen Brown 		fp = fopen_or_die("/proc/interrupts", "r");
3117562a2d37SLen Brown 	else
3118562a2d37SLen Brown 		rewind(fp);
3119562a2d37SLen Brown 
3120562a2d37SLen Brown 	/* read 1st line of /proc/interrupts to get cpu* name for each column */
3121562a2d37SLen Brown 	for (column = 0; column < topo.num_cpus; ++column) {
3122562a2d37SLen Brown 		int cpu_number;
3123562a2d37SLen Brown 
3124562a2d37SLen Brown 		retval = fscanf(fp, " CPU%d", &cpu_number);
3125562a2d37SLen Brown 		if (retval != 1)
3126562a2d37SLen Brown 			break;
3127562a2d37SLen Brown 
3128562a2d37SLen Brown 		if (cpu_number > topo.max_cpu_num) {
3129562a2d37SLen Brown 			warn("/proc/interrupts: cpu%d: > %d", cpu_number, topo.max_cpu_num);
3130562a2d37SLen Brown 			return 1;
3131562a2d37SLen Brown 		}
3132562a2d37SLen Brown 
3133562a2d37SLen Brown 		irq_column_2_cpu[column] = cpu_number;
3134562a2d37SLen Brown 		irqs_per_cpu[cpu_number] = 0;
3135562a2d37SLen Brown 	}
3136562a2d37SLen Brown 
3137562a2d37SLen Brown 	/* read /proc/interrupt count lines and sum up irqs per cpu */
3138562a2d37SLen Brown 	while (1) {
3139562a2d37SLen Brown 		int column;
3140562a2d37SLen Brown 		char buf[64];
3141562a2d37SLen Brown 
3142562a2d37SLen Brown 		retval = fscanf(fp, " %s:", buf);	/* flush irq# "N:" */
3143562a2d37SLen Brown 		if (retval != 1)
3144562a2d37SLen Brown 			break;
3145562a2d37SLen Brown 
3146562a2d37SLen Brown 		/* read the count per cpu */
3147562a2d37SLen Brown 		for (column = 0; column < topo.num_cpus; ++column) {
3148562a2d37SLen Brown 
3149562a2d37SLen Brown 			int cpu_number, irq_count;
3150562a2d37SLen Brown 
3151562a2d37SLen Brown 			retval = fscanf(fp, " %d", &irq_count);
3152562a2d37SLen Brown 			if (retval != 1)
3153562a2d37SLen Brown 				break;
3154562a2d37SLen Brown 
3155562a2d37SLen Brown 			cpu_number = irq_column_2_cpu[column];
3156562a2d37SLen Brown 			irqs_per_cpu[cpu_number] += irq_count;
3157562a2d37SLen Brown 
3158562a2d37SLen Brown 		}
3159562a2d37SLen Brown 
31601b439f01SLen Brown 		while (getc(fp) != '\n') ;	/* flush interrupt description */
3161562a2d37SLen Brown 
3162562a2d37SLen Brown 	}
3163562a2d37SLen Brown 	return 0;
3164562a2d37SLen Brown }
31651b439f01SLen Brown 
316627d47356SLen Brown /*
3167fdf676e5SLen Brown  * snapshot_gfx_rc6_ms()
3168fdf676e5SLen Brown  *
3169fdf676e5SLen Brown  * record snapshot of
3170fdf676e5SLen Brown  * /sys/class/drm/card0/power/rc6_residency_ms
3171fdf676e5SLen Brown  *
3172fdf676e5SLen Brown  * return 1 if config change requires a restart, else return 0
3173fdf676e5SLen Brown  */
3174fdf676e5SLen Brown int snapshot_gfx_rc6_ms(void)
3175fdf676e5SLen Brown {
3176fdf676e5SLen Brown 	FILE *fp;
3177fdf676e5SLen Brown 	int retval;
3178fdf676e5SLen Brown 
3179fdf676e5SLen Brown 	fp = fopen_or_die("/sys/class/drm/card0/power/rc6_residency_ms", "r");
3180fdf676e5SLen Brown 
3181fdf676e5SLen Brown 	retval = fscanf(fp, "%lld", &gfx_cur_rc6_ms);
3182fdf676e5SLen Brown 	if (retval != 1)
3183fdf676e5SLen Brown 		err(1, "GFX rc6");
3184fdf676e5SLen Brown 
3185fdf676e5SLen Brown 	fclose(fp);
3186fdf676e5SLen Brown 
3187fdf676e5SLen Brown 	return 0;
3188fdf676e5SLen Brown }
31891b439f01SLen Brown 
3190fdf676e5SLen Brown /*
319127d47356SLen Brown  * snapshot_gfx_mhz()
319227d47356SLen Brown  *
319327d47356SLen Brown  * record snapshot of
319427d47356SLen Brown  * /sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz
319527d47356SLen Brown  *
319627d47356SLen Brown  * return 1 if config change requires a restart, else return 0
319727d47356SLen Brown  */
319827d47356SLen Brown int snapshot_gfx_mhz(void)
319927d47356SLen Brown {
320027d47356SLen Brown 	static FILE *fp;
320127d47356SLen Brown 	int retval;
320227d47356SLen Brown 
320327d47356SLen Brown 	if (fp == NULL)
320427d47356SLen Brown 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", "r");
320522048c54SLen Brown 	else {
320627d47356SLen Brown 		rewind(fp);
320722048c54SLen Brown 		fflush(fp);
320822048c54SLen Brown 	}
320927d47356SLen Brown 
321027d47356SLen Brown 	retval = fscanf(fp, "%d", &gfx_cur_mhz);
321127d47356SLen Brown 	if (retval != 1)
321227d47356SLen Brown 		err(1, "GFX MHz");
321327d47356SLen Brown 
321427d47356SLen Brown 	return 0;
321527d47356SLen Brown }
3216562a2d37SLen Brown 
3217562a2d37SLen Brown /*
3218b4b91569SRafael Antognolli  * snapshot_gfx_cur_mhz()
3219b4b91569SRafael Antognolli  *
3220b4b91569SRafael Antognolli  * record snapshot of
3221b4b91569SRafael Antognolli  * /sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz
3222b4b91569SRafael Antognolli  *
3223b4b91569SRafael Antognolli  * return 1 if config change requires a restart, else return 0
3224b4b91569SRafael Antognolli  */
3225b4b91569SRafael Antognolli int snapshot_gfx_act_mhz(void)
3226b4b91569SRafael Antognolli {
3227b4b91569SRafael Antognolli 	static FILE *fp;
3228b4b91569SRafael Antognolli 	int retval;
3229b4b91569SRafael Antognolli 
3230b4b91569SRafael Antognolli 	if (fp == NULL)
3231b4b91569SRafael Antognolli 		fp = fopen_or_die("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", "r");
3232b4b91569SRafael Antognolli 	else {
3233b4b91569SRafael Antognolli 		rewind(fp);
3234b4b91569SRafael Antognolli 		fflush(fp);
3235b4b91569SRafael Antognolli 	}
3236b4b91569SRafael Antognolli 
3237b4b91569SRafael Antognolli 	retval = fscanf(fp, "%d", &gfx_act_mhz);
3238b4b91569SRafael Antognolli 	if (retval != 1)
3239b4b91569SRafael Antognolli 		err(1, "GFX ACT MHz");
3240b4b91569SRafael Antognolli 
3241b4b91569SRafael Antognolli 	return 0;
3242b4b91569SRafael Antognolli }
3243b4b91569SRafael Antognolli 
3244b4b91569SRafael Antognolli /*
3245be0e54c4SLen Brown  * snapshot_cpu_lpi()
3246be0e54c4SLen Brown  *
3247be0e54c4SLen Brown  * record snapshot of
3248be0e54c4SLen Brown  * /sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us
3249be0e54c4SLen Brown  */
3250be0e54c4SLen Brown int snapshot_cpu_lpi_us(void)
3251be0e54c4SLen Brown {
3252be0e54c4SLen Brown 	FILE *fp;
3253be0e54c4SLen Brown 	int retval;
3254be0e54c4SLen Brown 
3255be0e54c4SLen Brown 	fp = fopen_or_die("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", "r");
3256be0e54c4SLen Brown 
3257be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_cpu_lpi_us);
32585ea7647bSPrarit Bhargava 	if (retval != 1) {
32595ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle CPU output\n");
32605ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_CPU_LPI);
3261605736c6SGustavo A. R. Silva 		fclose(fp);
32625ea7647bSPrarit Bhargava 		return -1;
32635ea7647bSPrarit Bhargava 	}
3264be0e54c4SLen Brown 
3265be0e54c4SLen Brown 	fclose(fp);
3266be0e54c4SLen Brown 
3267be0e54c4SLen Brown 	return 0;
3268be0e54c4SLen Brown }
32691b439f01SLen Brown 
3270be0e54c4SLen Brown /*
3271be0e54c4SLen Brown  * snapshot_sys_lpi()
3272be0e54c4SLen Brown  *
32731f81c5efSLen Brown  * record snapshot of sys_lpi_file
3274be0e54c4SLen Brown  */
3275be0e54c4SLen Brown int snapshot_sys_lpi_us(void)
3276be0e54c4SLen Brown {
3277be0e54c4SLen Brown 	FILE *fp;
3278be0e54c4SLen Brown 	int retval;
3279be0e54c4SLen Brown 
32801f81c5efSLen Brown 	fp = fopen_or_die(sys_lpi_file, "r");
3281be0e54c4SLen Brown 
3282be0e54c4SLen Brown 	retval = fscanf(fp, "%lld", &cpuidle_cur_sys_lpi_us);
32835ea7647bSPrarit Bhargava 	if (retval != 1) {
32845ea7647bSPrarit Bhargava 		fprintf(stderr, "Disabling Low Power Idle System output\n");
32855ea7647bSPrarit Bhargava 		BIC_NOT_PRESENT(BIC_SYS_LPI);
328615423b95SColin Ian King 		fclose(fp);
32875ea7647bSPrarit Bhargava 		return -1;
32885ea7647bSPrarit Bhargava 	}
3289be0e54c4SLen Brown 	fclose(fp);
3290be0e54c4SLen Brown 
3291be0e54c4SLen Brown 	return 0;
3292be0e54c4SLen Brown }
32931b439f01SLen Brown 
3294be0e54c4SLen Brown /*
3295562a2d37SLen Brown  * snapshot /proc and /sys files
3296562a2d37SLen Brown  *
3297562a2d37SLen Brown  * return 1 if configuration restart needed, else return 0
3298562a2d37SLen Brown  */
3299562a2d37SLen Brown int snapshot_proc_sysfs_files(void)
3300562a2d37SLen Brown {
3301218f0e8dSLen Brown 	if (DO_BIC(BIC_IRQ))
3302562a2d37SLen Brown 		if (snapshot_proc_interrupts())
3303562a2d37SLen Brown 			return 1;
3304562a2d37SLen Brown 
3305812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
3306fdf676e5SLen Brown 		snapshot_gfx_rc6_ms();
3307fdf676e5SLen Brown 
3308812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
330927d47356SLen Brown 		snapshot_gfx_mhz();
331027d47356SLen Brown 
3311b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
3312b4b91569SRafael Antognolli 		snapshot_gfx_act_mhz();
3313b4b91569SRafael Antognolli 
3314be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
3315be0e54c4SLen Brown 		snapshot_cpu_lpi_us();
3316be0e54c4SLen Brown 
3317be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
3318be0e54c4SLen Brown 		snapshot_sys_lpi_us();
3319be0e54c4SLen Brown 
3320562a2d37SLen Brown 	return 0;
3321562a2d37SLen Brown }
3322562a2d37SLen Brown 
33238aa2ed0bSLen Brown int exit_requested;
33248aa2ed0bSLen Brown 
33258aa2ed0bSLen Brown static void signal_handler(int signal)
33268aa2ed0bSLen Brown {
33278aa2ed0bSLen Brown 	switch (signal) {
33288aa2ed0bSLen Brown 	case SIGINT:
33298aa2ed0bSLen Brown 		exit_requested = 1;
33308aa2ed0bSLen Brown 		if (debug)
33318aa2ed0bSLen Brown 			fprintf(stderr, " SIGINT\n");
33328aa2ed0bSLen Brown 		break;
333307211960SLen Brown 	case SIGUSR1:
333407211960SLen Brown 		if (debug > 1)
333507211960SLen Brown 			fprintf(stderr, "SIGUSR1\n");
333607211960SLen Brown 		break;
33378aa2ed0bSLen Brown 	}
33388aa2ed0bSLen Brown }
33398aa2ed0bSLen Brown 
33408aa2ed0bSLen Brown void setup_signal_handler(void)
33418aa2ed0bSLen Brown {
33428aa2ed0bSLen Brown 	struct sigaction sa;
33438aa2ed0bSLen Brown 
33448aa2ed0bSLen Brown 	memset(&sa, 0, sizeof(sa));
33458aa2ed0bSLen Brown 
33468aa2ed0bSLen Brown 	sa.sa_handler = &signal_handler;
33478aa2ed0bSLen Brown 
33488aa2ed0bSLen Brown 	if (sigaction(SIGINT, &sa, NULL) < 0)
33498aa2ed0bSLen Brown 		err(1, "sigaction SIGINT");
335007211960SLen Brown 	if (sigaction(SIGUSR1, &sa, NULL) < 0)
335107211960SLen Brown 		err(1, "sigaction SIGUSR1");
33528aa2ed0bSLen Brown }
3353b9ad8ee0SLen Brown 
335447936f94SArtem Bityutskiy void do_sleep(void)
3355b9ad8ee0SLen Brown {
3356c026c236SArtem Bityutskiy 	struct timeval tout;
3357c026c236SArtem Bityutskiy 	struct timespec rest;
3358b9ad8ee0SLen Brown 	fd_set readfds;
3359b9ad8ee0SLen Brown 	int retval;
3360b9ad8ee0SLen Brown 
3361b9ad8ee0SLen Brown 	FD_ZERO(&readfds);
3362b9ad8ee0SLen Brown 	FD_SET(0, &readfds);
3363b9ad8ee0SLen Brown 
3364c026c236SArtem Bityutskiy 	if (ignore_stdin) {
336547936f94SArtem Bityutskiy 		nanosleep(&interval_ts, NULL);
336647936f94SArtem Bityutskiy 		return;
336747936f94SArtem Bityutskiy 	}
3368b9ad8ee0SLen Brown 
3369c026c236SArtem Bityutskiy 	tout = interval_tv;
3370c026c236SArtem Bityutskiy 	retval = select(1, &readfds, NULL, NULL, &tout);
3371b9ad8ee0SLen Brown 
3372b9ad8ee0SLen Brown 	if (retval == 1) {
3373b9ad8ee0SLen Brown 		switch (getc(stdin)) {
3374b9ad8ee0SLen Brown 		case 'q':
3375b9ad8ee0SLen Brown 			exit_requested = 1;
3376b9ad8ee0SLen Brown 			break;
3377c026c236SArtem Bityutskiy 		case EOF:
3378c026c236SArtem Bityutskiy 			/*
3379c026c236SArtem Bityutskiy 			 * 'stdin' is a pipe closed on the other end. There
3380c026c236SArtem Bityutskiy 			 * won't be any further input.
3381c026c236SArtem Bityutskiy 			 */
3382c026c236SArtem Bityutskiy 			ignore_stdin = 1;
3383c026c236SArtem Bityutskiy 			/* Sleep the rest of the time */
3384c026c236SArtem Bityutskiy 			rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3385c026c236SArtem Bityutskiy 			rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3386c026c236SArtem Bityutskiy 			nanosleep(&rest, NULL);
3387b9ad8ee0SLen Brown 		}
3388b9ad8ee0SLen Brown 	}
3389b9ad8ee0SLen Brown }
339047936f94SArtem Bityutskiy 
339187e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr)
339287e15da9SChen Yu {
339387e15da9SChen Yu 	int ret, idx;
339487e15da9SChen Yu 	unsigned long long msr_cur, msr_last;
339587e15da9SChen Yu 
339687e15da9SChen Yu 	if (!per_cpu_msr_sum)
339787e15da9SChen Yu 		return 1;
339887e15da9SChen Yu 
339987e15da9SChen Yu 	idx = offset_to_idx(offset);
340087e15da9SChen Yu 	if (idx < 0)
340187e15da9SChen Yu 		return idx;
340287e15da9SChen Yu 	/* get_msr_sum() = sum + (get_msr() - last) */
340387e15da9SChen Yu 	ret = get_msr(cpu, offset, &msr_cur);
340487e15da9SChen Yu 	if (ret)
340587e15da9SChen Yu 		return ret;
340687e15da9SChen Yu 	msr_last = per_cpu_msr_sum[cpu].entries[idx].last;
340787e15da9SChen Yu 	DELTA_WRAP32(msr_cur, msr_last);
340887e15da9SChen Yu 	*msr = msr_last + per_cpu_msr_sum[cpu].entries[idx].sum;
340987e15da9SChen Yu 
341087e15da9SChen Yu 	return 0;
341187e15da9SChen Yu }
341287e15da9SChen Yu 
341387e15da9SChen Yu timer_t timerid;
341487e15da9SChen Yu 
341587e15da9SChen Yu /* Timer callback, update the sum of MSRs periodically. */
341687e15da9SChen Yu static int update_msr_sum(struct thread_data *t, struct core_data *c, struct pkg_data *p)
341787e15da9SChen Yu {
341887e15da9SChen Yu 	int i, ret;
341987e15da9SChen Yu 	int cpu = t->cpu_id;
342087e15da9SChen Yu 
342187e15da9SChen Yu 	for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
342287e15da9SChen Yu 		unsigned long long msr_cur, msr_last;
342313a779deSCalvin Walton 		off_t offset;
342487e15da9SChen Yu 
342587e15da9SChen Yu 		if (!idx_valid(i))
342687e15da9SChen Yu 			continue;
342787e15da9SChen Yu 		offset = idx_to_offset(i);
342887e15da9SChen Yu 		if (offset < 0)
342987e15da9SChen Yu 			continue;
343087e15da9SChen Yu 		ret = get_msr(cpu, offset, &msr_cur);
343187e15da9SChen Yu 		if (ret) {
34321b439f01SLen Brown 			fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset);
343387e15da9SChen Yu 			continue;
343487e15da9SChen Yu 		}
343587e15da9SChen Yu 
343687e15da9SChen Yu 		msr_last = per_cpu_msr_sum[cpu].entries[i].last;
343787e15da9SChen Yu 		per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
343887e15da9SChen Yu 
343987e15da9SChen Yu 		DELTA_WRAP32(msr_cur, msr_last);
344087e15da9SChen Yu 		per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
344187e15da9SChen Yu 	}
344287e15da9SChen Yu 	return 0;
344387e15da9SChen Yu }
344487e15da9SChen Yu 
34451b439f01SLen Brown static void msr_record_handler(union sigval v)
344687e15da9SChen Yu {
344787e15da9SChen Yu 	for_all_cpus(update_msr_sum, EVEN_COUNTERS);
344887e15da9SChen Yu }
344987e15da9SChen Yu 
345087e15da9SChen Yu void msr_sum_record(void)
345187e15da9SChen Yu {
345287e15da9SChen Yu 	struct itimerspec its;
345387e15da9SChen Yu 	struct sigevent sev;
345487e15da9SChen Yu 
345587e15da9SChen Yu 	per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
345687e15da9SChen Yu 	if (!per_cpu_msr_sum) {
345787e15da9SChen Yu 		fprintf(outf, "Can not allocate memory for long time MSR.\n");
345887e15da9SChen Yu 		return;
345987e15da9SChen Yu 	}
346087e15da9SChen Yu 	/*
346187e15da9SChen Yu 	 * Signal handler might be restricted, so use thread notifier instead.
346287e15da9SChen Yu 	 */
346387e15da9SChen Yu 	memset(&sev, 0, sizeof(struct sigevent));
346487e15da9SChen Yu 	sev.sigev_notify = SIGEV_THREAD;
346587e15da9SChen Yu 	sev.sigev_notify_function = msr_record_handler;
346687e15da9SChen Yu 
346787e15da9SChen Yu 	sev.sigev_value.sival_ptr = &timerid;
346887e15da9SChen Yu 	if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
346987e15da9SChen Yu 		fprintf(outf, "Can not create timer.\n");
347087e15da9SChen Yu 		goto release_msr;
347187e15da9SChen Yu 	}
347287e15da9SChen Yu 
347387e15da9SChen Yu 	its.it_value.tv_sec = 0;
347487e15da9SChen Yu 	its.it_value.tv_nsec = 1;
347587e15da9SChen Yu 	/*
347687e15da9SChen Yu 	 * A wraparound time has been calculated early.
347787e15da9SChen Yu 	 * Some sources state that the peak power for a
347887e15da9SChen Yu 	 * microprocessor is usually 1.5 times the TDP rating,
347987e15da9SChen Yu 	 * use 2 * TDP for safety.
348087e15da9SChen Yu 	 */
348187e15da9SChen Yu 	its.it_interval.tv_sec = rapl_joule_counter_range / 2;
348287e15da9SChen Yu 	its.it_interval.tv_nsec = 0;
348387e15da9SChen Yu 
348487e15da9SChen Yu 	if (timer_settime(timerid, 0, &its, NULL) == -1) {
348587e15da9SChen Yu 		fprintf(outf, "Can not set timer.\n");
348687e15da9SChen Yu 		goto release_timer;
348787e15da9SChen Yu 	}
348887e15da9SChen Yu 	return;
348987e15da9SChen Yu 
349087e15da9SChen Yu release_timer:
349187e15da9SChen Yu 	timer_delete(timerid);
349287e15da9SChen Yu release_msr:
349387e15da9SChen Yu 	free(per_cpu_msr_sum);
349487e15da9SChen Yu }
34954c2122d4SLen Brown 
349638c6663aSLen Brown /*
349738c6663aSLen Brown  * set_my_sched_priority(pri)
349838c6663aSLen Brown  * return previous
3499*164d7a96SLen Brown  *
3500*164d7a96SLen Brown  * if non-root, do this:
3501*164d7a96SLen Brown  * # /sbin/setcap cap_sys_rawio,cap_sys_nice=+ep /usr/bin/turbostat
350238c6663aSLen Brown  */
350338c6663aSLen Brown int set_my_sched_priority(int priority)
350438c6663aSLen Brown {
350538c6663aSLen Brown 	int retval;
350638c6663aSLen Brown 	int original_priority;
350738c6663aSLen Brown 
350838c6663aSLen Brown 	errno = 0;
350938c6663aSLen Brown 	original_priority = getpriority(PRIO_PROCESS, 0);
351038c6663aSLen Brown 	if (errno && (original_priority == -1))
351138c6663aSLen Brown 		err(errno, "getpriority");
351238c6663aSLen Brown 
351338c6663aSLen Brown 	retval = setpriority(PRIO_PROCESS, 0, priority);
351438c6663aSLen Brown 	if (retval)
351538c6663aSLen Brown 		err(retval, "setpriority(%d)", priority);
351638c6663aSLen Brown 
351738c6663aSLen Brown 	errno = 0;
351838c6663aSLen Brown 	retval = getpriority(PRIO_PROCESS, 0);
351938c6663aSLen Brown 	if (retval != priority)
3520*164d7a96SLen Brown 		err(retval, "getpriority(%d) != setpriority(%d)", retval, priority);
352138c6663aSLen Brown 
352238c6663aSLen Brown 	return original_priority;
352338c6663aSLen Brown }
352438c6663aSLen Brown 
3525103a8feaSLen Brown void turbostat_loop()
3526103a8feaSLen Brown {
3527c98d5d94SLen Brown 	int retval;
3528e52966c0SLen Brown 	int restarted = 0;
3529023fe0acSChen Yu 	int done_iters = 0;
3530c98d5d94SLen Brown 
35318aa2ed0bSLen Brown 	setup_signal_handler();
35328aa2ed0bSLen Brown 
353338c6663aSLen Brown 	/*
353438c6663aSLen Brown 	 * elevate own priority for interval mode
353538c6663aSLen Brown 	 */
353638c6663aSLen Brown 	set_my_sched_priority(-20);
353738c6663aSLen Brown 
3538103a8feaSLen Brown restart:
3539e52966c0SLen Brown 	restarted++;
3540e52966c0SLen Brown 
3541562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3542c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
35434c2122d4SLen Brown 	first_counter_read = 0;
3544d91bb17cSLen Brown 	if (retval < -1) {
3545d91bb17cSLen Brown 		exit(retval);
3546d91bb17cSLen Brown 	} else if (retval == -1) {
35473d7772eaSLen Brown 		if (restarted > 10) {
3548e52966c0SLen Brown 			exit(retval);
3549e52966c0SLen Brown 		}
3550c98d5d94SLen Brown 		re_initialize();
3551c98d5d94SLen Brown 		goto restart;
3552c98d5d94SLen Brown 	}
3553e52966c0SLen Brown 	restarted = 0;
3554023fe0acSChen Yu 	done_iters = 0;
3555103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3556103a8feaSLen Brown 
3557103a8feaSLen Brown 	while (1) {
3558c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3559103a8feaSLen Brown 			re_initialize();
3560103a8feaSLen Brown 			goto restart;
3561103a8feaSLen Brown 		}
3562b9ad8ee0SLen Brown 		do_sleep();
3563562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3564562a2d37SLen Brown 			goto restart;
3565c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3566d91bb17cSLen Brown 		if (retval < -1) {
3567d91bb17cSLen Brown 			exit(retval);
3568d91bb17cSLen Brown 		} else if (retval == -1) {
356915aaa346SLen Brown 			re_initialize();
357015aaa346SLen Brown 			goto restart;
357115aaa346SLen Brown 		}
3572103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3573103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3574ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3575ba3dec99SLen Brown 			re_initialize();
3576ba3dec99SLen Brown 			goto restart;
3577ba3dec99SLen Brown 		}
3578c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3579c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3580b7d8c148SLen Brown 		flush_output_stdout();
35818aa2ed0bSLen Brown 		if (exit_requested)
35828aa2ed0bSLen Brown 			break;
3583023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3584023fe0acSChen Yu 			break;
3585b9ad8ee0SLen Brown 		do_sleep();
3586562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3587562a2d37SLen Brown 			goto restart;
3588c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3589d91bb17cSLen Brown 		if (retval < -1) {
3590d91bb17cSLen Brown 			exit(retval);
3591d91bb17cSLen Brown 		} else if (retval == -1) {
3592103a8feaSLen Brown 			re_initialize();
3593103a8feaSLen Brown 			goto restart;
3594103a8feaSLen Brown 		}
3595103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3596103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3597ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3598ba3dec99SLen Brown 			re_initialize();
3599ba3dec99SLen Brown 			goto restart;
3600ba3dec99SLen Brown 		}
3601c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3602c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3603b7d8c148SLen Brown 		flush_output_stdout();
36048aa2ed0bSLen Brown 		if (exit_requested)
36058aa2ed0bSLen Brown 			break;
3606023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3607023fe0acSChen Yu 			break;
3608103a8feaSLen Brown 	}
3609103a8feaSLen Brown }
3610103a8feaSLen Brown 
3611103a8feaSLen Brown void check_dev_msr()
3612103a8feaSLen Brown {
3613103a8feaSLen Brown 	struct stat sb;
36147ce7d5deSPrarit Bhargava 	char pathname[32];
3615103a8feaSLen Brown 
36167ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36177ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3618a21d38c8SLen Brown 		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3619d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3620103a8feaSLen Brown }
3621103a8feaSLen Brown 
3622fcaa681cSLen Brown /*
3623fcaa681cSLen Brown  * check for CAP_SYS_RAWIO
3624fcaa681cSLen Brown  * return 0 on success
3625fcaa681cSLen Brown  * return 1 on fail
3626fcaa681cSLen Brown  */
3627fcaa681cSLen Brown int check_for_cap_sys_rawio(void)
3628103a8feaSLen Brown {
3629fcaa681cSLen Brown 	cap_t caps;
3630fcaa681cSLen Brown 	cap_flag_value_t cap_flag_value;
3631fcaa681cSLen Brown 
3632fcaa681cSLen Brown 	caps = cap_get_proc();
3633fcaa681cSLen Brown 	if (caps == NULL)
3634fcaa681cSLen Brown 		err(-6, "cap_get_proc\n");
3635fcaa681cSLen Brown 
3636fcaa681cSLen Brown 	if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3637fcaa681cSLen Brown 		err(-6, "cap_get\n");
3638fcaa681cSLen Brown 
3639fcaa681cSLen Brown 	if (cap_flag_value != CAP_SET) {
36401b439f01SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3641fcaa681cSLen Brown 		return 1;
3642fcaa681cSLen Brown 	}
3643fcaa681cSLen Brown 
3644fcaa681cSLen Brown 	if (cap_free(caps) == -1)
3645fcaa681cSLen Brown 		err(-6, "cap_free\n");
3646fcaa681cSLen Brown 
3647fcaa681cSLen Brown 	return 0;
3648fcaa681cSLen Brown }
36491b439f01SLen Brown 
3650fcaa681cSLen Brown void check_permissions(void)
3651fcaa681cSLen Brown {
365298481e79SLen Brown 	int do_exit = 0;
36537ce7d5deSPrarit Bhargava 	char pathname[32];
365498481e79SLen Brown 
365598481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
3656fcaa681cSLen Brown 	do_exit += check_for_cap_sys_rawio();
365798481e79SLen Brown 
365898481e79SLen Brown 	/* test file permissions */
36597ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36607ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
366198481e79SLen Brown 		do_exit++;
366298481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
366398481e79SLen Brown 	}
366498481e79SLen Brown 
366598481e79SLen Brown 	/* if all else fails, thell them to be root */
366698481e79SLen Brown 	if (do_exit)
3667b2c95d90SJosh Triplett 		if (getuid() != 0)
3668d7899447SLen Brown 			warnx("... or simply run as root");
366998481e79SLen Brown 
367098481e79SLen Brown 	if (do_exit)
367198481e79SLen Brown 		exit(-6);
3672103a8feaSLen Brown }
3673103a8feaSLen Brown 
3674d7899447SLen Brown /*
3675d7899447SLen Brown  * NHM adds support for additional MSRs:
3676d7899447SLen Brown  *
3677d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3678d7899447SLen Brown  *
3679ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
36801df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3681d7899447SLen Brown  *
3682cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3683cf4cbe53SLen Brown  *
3684d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3685d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3686d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3687d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3688d7899447SLen Brown  *
3689ee7e38e3SLen Brown  * Side effect:
36901df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
369133148d67SLen Brown  * sets has_misc_feature_control
3692d7899447SLen Brown  */
3693ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3694103a8feaSLen Brown {
3695ee7e38e3SLen Brown 	unsigned long long msr;
369621ed5574SLen Brown 	unsigned int base_ratio;
3697ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3698ee7e38e3SLen Brown 
3699103a8feaSLen Brown 	if (!genuine_intel)
3700103a8feaSLen Brown 		return 0;
3701103a8feaSLen Brown 
3702103a8feaSLen Brown 	if (family != 6)
3703103a8feaSLen Brown 		return 0;
3704103a8feaSLen Brown 
370521ed5574SLen Brown 	bclk = discover_bclk(family, model);
370621ed5574SLen Brown 
3707103a8feaSLen Brown 	switch (model) {
3708869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3709869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3710ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3711ee7e38e3SLen Brown 		break;
3712869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3713869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3714869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3715869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3716ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
371733148d67SLen Brown 		has_misc_feature_control = 1;
3718ee7e38e3SLen Brown 		break;
3719c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
37205e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3721869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
372277e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
3723c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
37245e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3725869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3726af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3727af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3728ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
372933148d67SLen Brown 		has_misc_feature_control = 1;
3730ee7e38e3SLen Brown 		break;
3731d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3732d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
373333148d67SLen Brown 		has_misc_feature_control = 1;
3734d8ebb442SLen Brown 		break;
373525368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
373625368d7cSChen Yu 		pkg_cstate_limits = icx_pkg_cstate_limits;
373725368d7cSChen Yu 		has_misc_feature_control = 1;
373825368d7cSChen Yu 		break;
3739f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3740cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
37415ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
3742ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3743ee7e38e3SLen Brown 		break;
3744869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3745ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3746cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3747ee7e38e3SLen Brown 		break;
3748869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3749ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3750ee7e38e3SLen Brown 		break;
3751869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3752f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
37535ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
3754f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
375520de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
3756445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3757e4085d54SLen Brown 		break;
3758103a8feaSLen Brown 	default:
3759103a8feaSLen Brown 		return 0;
3760103a8feaSLen Brown 	}
37611df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3762e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3763ee7e38e3SLen Brown 
3764ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
376521ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
376621ed5574SLen Brown 
376721ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
376821ed5574SLen Brown 	has_base_hz = 1;
3769ee7e38e3SLen Brown 	return 1;
3770103a8feaSLen Brown }
37711b439f01SLen Brown 
37720f7887c4SLen Brown /*
3773495c7654SLen Brown  * SLV client has support for unique MSRs:
37740f7887c4SLen Brown  *
37750f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
37760f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
37770f7887c4SLen Brown  */
37780f7887c4SLen Brown 
37790f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
37800f7887c4SLen Brown {
37810f7887c4SLen Brown 	if (!genuine_intel)
37820f7887c4SLen Brown 		return 0;
37830f7887c4SLen Brown 
37840f7887c4SLen Brown 	switch (model) {
3785f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3786f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3787f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
37880f7887c4SLen Brown 		return 1;
37890f7887c4SLen Brown 	}
37900f7887c4SLen Brown 	return 0;
37910f7887c4SLen Brown }
37921b439f01SLen Brown 
37937170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
37947170a374SLen Brown {
37957170a374SLen Brown 
37967170a374SLen Brown 	if (!genuine_intel)
37977170a374SLen Brown 		return 0;
37987170a374SLen Brown 
37997170a374SLen Brown 	switch (model) {
38005ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
38017170a374SLen Brown 		return 1;
38027170a374SLen Brown 	}
38037170a374SLen Brown 	return 0;
38047170a374SLen Brown }
38051b439f01SLen Brown 
3806ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3807ade0ebacSLen Brown {
3808ade0ebacSLen Brown 
3809ade0ebacSLen Brown 	if (!genuine_intel)
3810ade0ebacSLen Brown 		return 0;
3811ade0ebacSLen Brown 
3812ade0ebacSLen Brown 	switch (model) {
3813ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3814ade0ebacSLen Brown 		return 1;
3815ade0ebacSLen Brown 	}
3816ade0ebacSLen Brown 	return 0;
3817ade0ebacSLen Brown }
38181b439f01SLen Brown 
381934c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
382034c76197SLen Brown {
382134c76197SLen Brown 
382234c76197SLen Brown 	if (!genuine_intel)
382334c76197SLen Brown 		return 0;
382434c76197SLen Brown 
382534c76197SLen Brown 	switch (model) {
382634c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
382734c76197SLen Brown 		return 1;
382834c76197SLen Brown 	}
382934c76197SLen Brown 	return 0;
383034c76197SLen Brown }
383125368d7cSChen Yu 
383225368d7cSChen Yu int is_icx(unsigned int family, unsigned int model)
383325368d7cSChen Yu {
383425368d7cSChen Yu 
383525368d7cSChen Yu 	if (!genuine_intel)
383625368d7cSChen Yu 		return 0;
383725368d7cSChen Yu 
383825368d7cSChen Yu 	switch (model) {
383925368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
384025368d7cSChen Yu 		return 1;
384125368d7cSChen Yu 	}
384225368d7cSChen Yu 	return 0;
384325368d7cSChen Yu }
384425368d7cSChen Yu 
3845f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model)
3846f6708400SChen Yu {
3847f6708400SChen Yu 	if (!genuine_intel)
3848f6708400SChen Yu 		return 0;
3849f6708400SChen Yu 
3850f6708400SChen Yu 	switch (model) {
3851f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:
3852f6708400SChen Yu 		return 1;
3853f6708400SChen Yu 	}
3854f6708400SChen Yu 	return 0;
3855f6708400SChen Yu }
38561b439f01SLen Brown 
385720de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model)
385820de0dabSAntti Laakso {
385920de0dabSAntti Laakso 	if (!genuine_intel)
386020de0dabSAntti Laakso 		return 0;
386120de0dabSAntti Laakso 
386220de0dabSAntti Laakso 	switch (model) {
386320de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:
386420de0dabSAntti Laakso 		return 1;
386520de0dabSAntti Laakso 	}
386620de0dabSAntti Laakso 	return 0;
386720de0dabSAntti Laakso }
38680f7887c4SLen Brown 
386931e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3870d7899447SLen Brown {
38710f7887c4SLen Brown 	if (has_slv_msrs(family, model))
38720f7887c4SLen Brown 		return 0;
38730f7887c4SLen Brown 
3874d7899447SLen Brown 	switch (model) {
3875d7899447SLen Brown 		/* Nehalem compatible, but do not include turbo-ratio limit support */
3876869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3877869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3878d7899447SLen Brown 		return 0;
3879d7899447SLen Brown 	default:
3880d7899447SLen Brown 		return 1;
3881d7899447SLen Brown 	}
3882d7899447SLen Brown }
38831b439f01SLen Brown 
38840f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
38850f7887c4SLen Brown {
38860f7887c4SLen Brown 	if (has_slv_msrs(family, model))
38870f7887c4SLen Brown 		return 1;
38880f7887c4SLen Brown 
38890f7887c4SLen Brown 	return 0;
38900f7887c4SLen Brown }
38911b439f01SLen Brown 
38926574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
38936574a5d5SLen Brown {
38946574a5d5SLen Brown 	if (!genuine_intel)
38956574a5d5SLen Brown 		return 0;
38966574a5d5SLen Brown 
38976574a5d5SLen Brown 	if (family != 6)
38986574a5d5SLen Brown 		return 0;
38996574a5d5SLen Brown 
39006574a5d5SLen Brown 	switch (model) {
3901869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3902869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
39036574a5d5SLen Brown 		return 1;
39046574a5d5SLen Brown 	default:
39056574a5d5SLen Brown 		return 0;
39066574a5d5SLen Brown 	}
39076574a5d5SLen Brown }
39081b439f01SLen Brown 
3909fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3910fcd17211SLen Brown {
3911fcd17211SLen Brown 	if (!genuine_intel)
3912fcd17211SLen Brown 		return 0;
3913fcd17211SLen Brown 
3914fcd17211SLen Brown 	if (family != 6)
3915fcd17211SLen Brown 		return 0;
3916fcd17211SLen Brown 
3917fcd17211SLen Brown 	switch (model) {
3918869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3919fcd17211SLen Brown 		return 1;
3920fcd17211SLen Brown 	default:
3921fcd17211SLen Brown 		return 0;
3922fcd17211SLen Brown 	}
3923fcd17211SLen Brown }
3924fcd17211SLen Brown 
3925fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3926fb5d4327SDasaratharaman Chandramouli {
3927fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3928fb5d4327SDasaratharaman Chandramouli 		return 0;
3929fb5d4327SDasaratharaman Chandramouli 
3930fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3931fb5d4327SDasaratharaman Chandramouli 		return 0;
3932fb5d4327SDasaratharaman Chandramouli 
3933fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3934869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3935fb5d4327SDasaratharaman Chandramouli 		return 1;
3936fb5d4327SDasaratharaman Chandramouli 	default:
3937fb5d4327SDasaratharaman Chandramouli 		return 0;
3938fb5d4327SDasaratharaman Chandramouli 	}
3939fb5d4327SDasaratharaman Chandramouli }
39401b439f01SLen Brown 
394131e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
394231e07522SLen Brown {
394331e07522SLen Brown 	if (!genuine_intel)
394431e07522SLen Brown 		return 0;
394531e07522SLen Brown 
394631e07522SLen Brown 	if (family != 6)
394731e07522SLen Brown 		return 0;
394831e07522SLen Brown 
394931e07522SLen Brown 	switch (model) {
395031e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
395131e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
395225368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
395331e07522SLen Brown 		return 1;
395431e07522SLen Brown 	default:
395531e07522SLen Brown 		return 0;
395631e07522SLen Brown 	}
395731e07522SLen Brown }
39581b439f01SLen Brown 
39596fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
39606fb3143bSLen Brown {
39616fb3143bSLen Brown 	if (!genuine_intel)
39626fb3143bSLen Brown 		return 0;
39636fb3143bSLen Brown 
39646fb3143bSLen Brown 	if (family != 6)
39656fb3143bSLen Brown 		return 0;
39666fb3143bSLen Brown 
39676fb3143bSLen Brown 	switch (model) {
3968869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3969c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
3970869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
397177e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
39725e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3973c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
39745e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3975869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3976af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3977af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3978869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
397925368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
39806fb3143bSLen Brown 
3981869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
39826fb3143bSLen Brown 		return 1;
39836fb3143bSLen Brown 	default:
39846fb3143bSLen Brown 		return 0;
39856fb3143bSLen Brown 	}
39866fb3143bSLen Brown }
39876fb3143bSLen Brown 
39880b9a0b9bSZhang Rui /*
39890b9a0b9bSZhang Rui  * tcc_offset_bits:
39900b9a0b9bSZhang Rui  * 0: Tcc Offset not supported (Default)
39910b9a0b9bSZhang Rui  * 6: Bit 29:24 of MSR_PLATFORM_INFO
39920b9a0b9bSZhang Rui  * 4: Bit 27:24 of MSR_PLATFORM_INFO
39930b9a0b9bSZhang Rui  */
39940b9a0b9bSZhang Rui void check_tcc_offset(int model)
39950b9a0b9bSZhang Rui {
39960b9a0b9bSZhang Rui 	unsigned long long msr;
39970b9a0b9bSZhang Rui 
39980b9a0b9bSZhang Rui 	if (!genuine_intel)
39990b9a0b9bSZhang Rui 		return;
40000b9a0b9bSZhang Rui 
40010b9a0b9bSZhang Rui 	switch (model) {
40020b9a0b9bSZhang Rui 	case INTEL_FAM6_SKYLAKE_L:
40030b9a0b9bSZhang Rui 	case INTEL_FAM6_SKYLAKE:
40040b9a0b9bSZhang Rui 	case INTEL_FAM6_KABYLAKE_L:
40050b9a0b9bSZhang Rui 	case INTEL_FAM6_KABYLAKE:
40060b9a0b9bSZhang Rui 	case INTEL_FAM6_ICELAKE_L:
40070b9a0b9bSZhang Rui 	case INTEL_FAM6_ICELAKE:
40080b9a0b9bSZhang Rui 	case INTEL_FAM6_TIGERLAKE_L:
40090b9a0b9bSZhang Rui 	case INTEL_FAM6_TIGERLAKE:
40100b9a0b9bSZhang Rui 	case INTEL_FAM6_COMETLAKE:
40110b9a0b9bSZhang Rui 		if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
40120b9a0b9bSZhang Rui 			msr = (msr >> 30) & 1;
40130b9a0b9bSZhang Rui 			if (msr)
40140b9a0b9bSZhang Rui 				tcc_offset_bits = 6;
40150b9a0b9bSZhang Rui 		}
40160b9a0b9bSZhang Rui 		return;
40170b9a0b9bSZhang Rui 	default:
40180b9a0b9bSZhang Rui 		return;
40190b9a0b9bSZhang Rui 	}
40200b9a0b9bSZhang Rui }
40210b9a0b9bSZhang Rui 
40221b439f01SLen Brown static void remove_underbar(char *s)
4023fecb3bc8SDavid Arcari {
4024fecb3bc8SDavid Arcari 	char *to = s;
4025fecb3bc8SDavid Arcari 
4026fecb3bc8SDavid Arcari 	while (*s) {
4027fecb3bc8SDavid Arcari 		if (*s != '_')
4028fecb3bc8SDavid Arcari 			*to++ = *s;
4029fecb3bc8SDavid Arcari 		s++;
4030fecb3bc8SDavid Arcari 	}
4031fecb3bc8SDavid Arcari 
4032fecb3bc8SDavid Arcari 	*to = 0;
4033fecb3bc8SDavid Arcari }
4034fecb3bc8SDavid Arcari 
40351b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
4036fcd17211SLen Brown {
4037fcd17211SLen Brown 	if (!do_nhm_platform_info)
4038fcd17211SLen Brown 		return;
4039fcd17211SLen Brown 
4040fcd17211SLen Brown 	dump_nhm_platform_info();
4041fcd17211SLen Brown 
4042fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
4043fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
4044fcd17211SLen Brown 
4045fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
4046fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
4047fcd17211SLen Brown 
404831e07522SLen Brown 	if (has_turbo_ratio_limit(family, model))
404931e07522SLen Brown 		dump_turbo_ratio_limits(family, model);
4050fcd17211SLen Brown 
40510f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
40520f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
40530f7887c4SLen Brown 
4054fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
4055fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
4056fb5d4327SDasaratharaman Chandramouli 
40576fb3143bSLen Brown 	if (has_config_tdp(family, model))
40586fb3143bSLen Brown 		dump_config_tdp();
40596fb3143bSLen Brown 
4060fcd17211SLen Brown 	dump_nhm_cst_cfg();
4061fcd17211SLen Brown }
4062fcd17211SLen Brown 
4063abdcbdb2SLen Brown static void dump_sysfs_file(char *path)
4064abdcbdb2SLen Brown {
4065abdcbdb2SLen Brown 	FILE *input;
4066abdcbdb2SLen Brown 	char cpuidle_buf[64];
4067abdcbdb2SLen Brown 
4068abdcbdb2SLen Brown 	input = fopen(path, "r");
4069abdcbdb2SLen Brown 	if (input == NULL) {
4070abdcbdb2SLen Brown 		if (debug)
4071abdcbdb2SLen Brown 			fprintf(outf, "NSFOD %s\n", path);
4072abdcbdb2SLen Brown 		return;
4073abdcbdb2SLen Brown 	}
4074abdcbdb2SLen Brown 	if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
4075abdcbdb2SLen Brown 		err(1, "%s: failed to read file", path);
4076abdcbdb2SLen Brown 	fclose(input);
4077abdcbdb2SLen Brown 
4078abdcbdb2SLen Brown 	fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
4079abdcbdb2SLen Brown }
40801b439f01SLen Brown 
40811b439f01SLen Brown static void dump_sysfs_cstate_config(void)
408241618e63SLen Brown {
408341618e63SLen Brown 	char path[64];
408441618e63SLen Brown 	char name_buf[16];
408541618e63SLen Brown 	char desc[64];
408641618e63SLen Brown 	FILE *input;
408741618e63SLen Brown 	int state;
408841618e63SLen Brown 	char *sp;
408941618e63SLen Brown 
4090abdcbdb2SLen Brown 	if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
4091abdcbdb2SLen Brown 		fprintf(outf, "cpuidle not loaded\n");
4092abdcbdb2SLen Brown 		return;
4093abdcbdb2SLen Brown 	}
4094abdcbdb2SLen Brown 
4095abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4096abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4097abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4098abdcbdb2SLen Brown 
409941618e63SLen Brown 	for (state = 0; state < 10; ++state) {
410041618e63SLen Brown 
41011b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
410241618e63SLen Brown 		input = fopen(path, "r");
410341618e63SLen Brown 		if (input == NULL)
410441618e63SLen Brown 			continue;
41058173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
41068173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
410741618e63SLen Brown 
410841618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
410941618e63SLen Brown 		sp = strchr(name_buf, '-');
411041618e63SLen Brown 		if (!sp)
411141618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
411241618e63SLen Brown 		*sp = '\0';
411341618e63SLen Brown 		fclose(input);
411441618e63SLen Brown 
4115fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
4116fecb3bc8SDavid Arcari 
41171b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state);
411841618e63SLen Brown 		input = fopen(path, "r");
411941618e63SLen Brown 		if (input == NULL)
412041618e63SLen Brown 			continue;
41218173c336SBen Hutchings 		if (!fgets(desc, sizeof(desc), input))
41228173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
412341618e63SLen Brown 
412441618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
412541618e63SLen Brown 		fclose(input);
412641618e63SLen Brown 	}
412741618e63SLen Brown }
41281b439f01SLen Brown 
41291b439f01SLen Brown static void dump_sysfs_pstate_config(void)
41307293fccdSLen Brown {
41317293fccdSLen Brown 	char path[64];
41327293fccdSLen Brown 	char driver_buf[64];
41337293fccdSLen Brown 	char governor_buf[64];
41347293fccdSLen Brown 	FILE *input;
41357293fccdSLen Brown 	int turbo;
41367293fccdSLen Brown 
41371b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu);
41387293fccdSLen Brown 	input = fopen(path, "r");
41397293fccdSLen Brown 	if (input == NULL) {
41400a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
41417293fccdSLen Brown 		return;
41427293fccdSLen Brown 	}
41438173c336SBen Hutchings 	if (!fgets(driver_buf, sizeof(driver_buf), input))
41448173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
41457293fccdSLen Brown 	fclose(input);
41467293fccdSLen Brown 
41471b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu);
41487293fccdSLen Brown 	input = fopen(path, "r");
41497293fccdSLen Brown 	if (input == NULL) {
41500a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
41517293fccdSLen Brown 		return;
41527293fccdSLen Brown 	}
41538173c336SBen Hutchings 	if (!fgets(governor_buf, sizeof(governor_buf), input))
41548173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
41557293fccdSLen Brown 	fclose(input);
41567293fccdSLen Brown 
41577293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
41587293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
41597293fccdSLen Brown 
41607293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
41617293fccdSLen Brown 	input = fopen(path, "r");
41627293fccdSLen Brown 	if (input != NULL) {
41638173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
41648173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
41657293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
41667293fccdSLen Brown 		fclose(input);
41677293fccdSLen Brown 	}
41687293fccdSLen Brown 
41697293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
41707293fccdSLen Brown 	input = fopen(path, "r");
41717293fccdSLen Brown 	if (input != NULL) {
41728173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
41738173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
41747293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
41757293fccdSLen Brown 		fclose(input);
41767293fccdSLen Brown 	}
41777293fccdSLen Brown }
417841618e63SLen Brown 
4179889facbeSLen Brown /*
4180889facbeSLen Brown  * print_epb()
4181889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
4182889facbeSLen Brown  */
4183889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4184889facbeSLen Brown {
4185889facbeSLen Brown 	char *epb_string;
41866d6501d9SBorislav Petkov 	int cpu, epb;
4187889facbeSLen Brown 
4188889facbeSLen Brown 	if (!has_epb)
4189889facbeSLen Brown 		return 0;
4190889facbeSLen Brown 
4191889facbeSLen Brown 	cpu = t->cpu_id;
4192889facbeSLen Brown 
4193889facbeSLen Brown 	/* EPB is per-package */
4194889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4195889facbeSLen Brown 		return 0;
4196889facbeSLen Brown 
4197889facbeSLen Brown 	if (cpu_migrate(cpu)) {
41983d7772eaSLen Brown 		fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4199889facbeSLen Brown 		return -1;
4200889facbeSLen Brown 	}
4201889facbeSLen Brown 
42026d6501d9SBorislav Petkov 	epb = get_epb(cpu);
42036d6501d9SBorislav Petkov 	if (epb < 0)
4204889facbeSLen Brown 		return 0;
4205889facbeSLen Brown 
42066d6501d9SBorislav Petkov 	switch (epb) {
4207889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
4208889facbeSLen Brown 		epb_string = "performance";
4209889facbeSLen Brown 		break;
4210889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
4211889facbeSLen Brown 		epb_string = "balanced";
4212889facbeSLen Brown 		break;
4213889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
4214889facbeSLen Brown 		epb_string = "powersave";
4215889facbeSLen Brown 		break;
4216889facbeSLen Brown 	default:
4217889facbeSLen Brown 		epb_string = "custom";
4218889facbeSLen Brown 		break;
4219889facbeSLen Brown 	}
42206d6501d9SBorislav Petkov 	fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4221889facbeSLen Brown 
4222889facbeSLen Brown 	return 0;
4223889facbeSLen Brown }
42241b439f01SLen Brown 
42257f5c258eSLen Brown /*
42267f5c258eSLen Brown  * print_hwp()
42277f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
42287f5c258eSLen Brown  */
42297f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
42307f5c258eSLen Brown {
42317f5c258eSLen Brown 	unsigned long long msr;
42327f5c258eSLen Brown 	int cpu;
42337f5c258eSLen Brown 
42347f5c258eSLen Brown 	if (!has_hwp)
42357f5c258eSLen Brown 		return 0;
42367f5c258eSLen Brown 
42377f5c258eSLen Brown 	cpu = t->cpu_id;
42387f5c258eSLen Brown 
42397f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
42407f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
42417f5c258eSLen Brown 		return 0;
42427f5c258eSLen Brown 
42437f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
42443d7772eaSLen Brown 		fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
42457f5c258eSLen Brown 		return -1;
42467f5c258eSLen Brown 	}
42477f5c258eSLen Brown 
42487f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
42497f5c258eSLen Brown 		return 0;
42507f5c258eSLen Brown 
42511b439f01SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-");
42527f5c258eSLen Brown 
42537f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
42547f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
42557f5c258eSLen Brown 		return 0;
42567f5c258eSLen Brown 
42577f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
42587f5c258eSLen Brown 		return 0;
42597f5c258eSLen Brown 
4260b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
42616dbd25a2SLen Brown 		"(high %d guar %d eff %d low %d)\n",
42627f5c258eSLen Brown 		cpu, msr,
42637f5c258eSLen Brown 		(unsigned int)HWP_HIGHEST_PERF(msr),
42647f5c258eSLen Brown 		(unsigned int)HWP_GUARANTEED_PERF(msr),
42651b439f01SLen Brown 		(unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr));
42667f5c258eSLen Brown 
42677f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
42687f5c258eSLen Brown 		return 0;
42697f5c258eSLen Brown 
4270b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
42716dbd25a2SLen Brown 		"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
42727f5c258eSLen Brown 		cpu, msr,
42737f5c258eSLen Brown 		(unsigned int)(((msr) >> 0) & 0xff),
42747f5c258eSLen Brown 		(unsigned int)(((msr) >> 8) & 0xff),
42757f5c258eSLen Brown 		(unsigned int)(((msr) >> 16) & 0xff),
42767f5c258eSLen Brown 		(unsigned int)(((msr) >> 24) & 0xff),
42771b439f01SLen Brown 		(unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1));
42787f5c258eSLen Brown 
42797f5c258eSLen Brown 	if (has_hwp_pkg) {
42807f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
42817f5c258eSLen Brown 			return 0;
42827f5c258eSLen Brown 
4283b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
42846dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
42857f5c258eSLen Brown 			cpu, msr,
42867f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
42877f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
42887f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
42891b439f01SLen Brown 			(unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3));
42907f5c258eSLen Brown 	}
42917f5c258eSLen Brown 	if (has_hwp_notify) {
42927f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
42937f5c258eSLen Brown 			return 0;
42947f5c258eSLen Brown 
4295b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
42967f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
42971b439f01SLen Brown 			cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis");
42987f5c258eSLen Brown 	}
42997f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
43007f5c258eSLen Brown 		return 0;
43017f5c258eSLen Brown 
4302b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
43037f5c258eSLen Brown 		"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
43041b439f01SLen Brown 		cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-");
4305889facbeSLen Brown 
4306889facbeSLen Brown 	return 0;
4307889facbeSLen Brown }
4308889facbeSLen Brown 
43093a9a941dSLen Brown /*
43103a9a941dSLen Brown  * print_perf_limit()
43113a9a941dSLen Brown  */
43123a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
43133a9a941dSLen Brown {
43143a9a941dSLen Brown 	unsigned long long msr;
43153a9a941dSLen Brown 	int cpu;
43163a9a941dSLen Brown 
43173a9a941dSLen Brown 	cpu = t->cpu_id;
43183a9a941dSLen Brown 
43193a9a941dSLen Brown 	/* per-package */
43203a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
43213a9a941dSLen Brown 		return 0;
43223a9a941dSLen Brown 
43233a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
43243d7772eaSLen Brown 		fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
43253a9a941dSLen Brown 		return -1;
43263a9a941dSLen Brown 	}
43273a9a941dSLen Brown 
43283a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
43293a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4330b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4331b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4332e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
43333a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
4334e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
4335e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4336e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
4337e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
4338e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
4339e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
4340e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
4341e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
4342e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
4343e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
43441b439f01SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : "");
4345b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4346e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
43473a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
4348e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
4349e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4350e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
4351e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
4352e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
4353e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
4354e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
4355e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
4356e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
4357e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
43581b439f01SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : "");
43593a9a941dSLen Brown 
43603a9a941dSLen Brown 	}
43613a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
43623a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4363b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4364b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
43653a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
43663a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
43673a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
43683a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
43693a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
43703a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
43711b439f01SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4372b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
43733a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
43743a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
43753a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
43763a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
43773a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
43783a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
43791b439f01SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
43803a9a941dSLen Brown 	}
43813a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
43823a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4383b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4384b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
43853a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
43863a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
43873a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
43883a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
43891b439f01SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4390b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
43913a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
43923a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
43933a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
43943a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
43951b439f01SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
43963a9a941dSLen Brown 	}
43973a9a941dSLen Brown 	return 0;
43983a9a941dSLen Brown }
43993a9a941dSLen Brown 
4400889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
4401889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F	/* 6 bit time granularity */
4402889facbeSLen Brown 
44039392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
4404144b44b1SLen Brown {
4405144b44b1SLen Brown 	unsigned long long msr;
4406144b44b1SLen Brown 
4407144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
44087ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4409144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4410144b44b1SLen Brown 
4411144b44b1SLen Brown 	switch (model) {
4412f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
44135ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:
4414144b44b1SLen Brown 		return 30.0;
4415144b44b1SLen Brown 	default:
4416144b44b1SLen Brown 		return 135.0;
4417144b44b1SLen Brown 	}
4418144b44b1SLen Brown }
4419144b44b1SLen Brown 
44209392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
44219392bd98SCalvin Walton {
442233eb8225SKim Phillips 	/* This is the max stock TDP of HEDT/Server Fam17h+ chips */
442333eb8225SKim Phillips 	return 280.0;
44249392bd98SCalvin Walton }
44259392bd98SCalvin Walton 
442640ee8e3bSAndrey Semin /*
442740ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
442840ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
442940ee8e3bSAndrey Semin  */
44301b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units)
443140ee8e3bSAndrey Semin {
443240ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
443340ee8e3bSAndrey Semin 
443440ee8e3bSAndrey Semin 	switch (model) {
4435869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
4436869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4437abdc75abSZhang Rui 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4438869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
44396397b641SLen Brown 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
444040ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
444140ee8e3bSAndrey Semin 	default:
444240ee8e3bSAndrey Semin 		return (rapl_energy_units);
444340ee8e3bSAndrey Semin 	}
444440ee8e3bSAndrey Semin }
444540ee8e3bSAndrey Semin 
44469392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
4447889facbeSLen Brown {
4448889facbeSLen Brown 	unsigned long long msr;
4449144b44b1SLen Brown 	unsigned int time_unit;
4450889facbeSLen Brown 	double tdp;
4451889facbeSLen Brown 
4452889facbeSLen Brown 	if (family != 6)
4453889facbeSLen Brown 		return;
4454889facbeSLen Brown 
4455889facbeSLen Brown 	switch (model) {
4456869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4457869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
4458c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
445977e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
44605e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
4461c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
44625e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
4463144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4464812db3f7SLen Brown 		if (rapl_joules) {
4465812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4466812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4467812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
4468812db3f7SLen Brown 		} else {
4469812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4470812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4471812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
4472812db3f7SLen Brown 		}
4473889facbeSLen Brown 		break;
4474869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4475f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4476e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4477812db3f7SLen Brown 		if (rapl_joules)
4478812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4479812db3f7SLen Brown 		else
4480812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4481e4085d54SLen Brown 		break;
4482f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
44831b439f01SLen Brown 		do_rapl =
44841b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
44851b439f01SLen Brown 		    | RAPL_GFX | RAPL_PKG_POWER_INFO;
4486f6708400SChen Yu 		if (rapl_joules) {
4487f6708400SChen Yu 			BIC_PRESENT(BIC_Pkg_J);
4488f6708400SChen Yu 			BIC_PRESENT(BIC_Cor_J);
4489f6708400SChen Yu 			BIC_PRESENT(BIC_RAM_J);
4490f6708400SChen Yu 			BIC_PRESENT(BIC_GFX_J);
4491f6708400SChen Yu 		} else {
4492f6708400SChen Yu 			BIC_PRESENT(BIC_PkgWatt);
4493f6708400SChen Yu 			BIC_PRESENT(BIC_CorWatt);
4494f6708400SChen Yu 			BIC_PRESENT(BIC_RAMWatt);
4495f6708400SChen Yu 			BIC_PRESENT(BIC_GFXWatt);
4496f6708400SChen Yu 		}
4497f6708400SChen Yu 		break;
449820de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
449920de0dabSAntti Laakso 		do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
450020de0dabSAntti Laakso 		BIC_PRESENT(BIC_PKG__);
450120de0dabSAntti Laakso 		if (rapl_joules)
450220de0dabSAntti Laakso 			BIC_PRESENT(BIC_Pkg_J);
450320de0dabSAntti Laakso 		else
450420de0dabSAntti Laakso 			BIC_PRESENT(BIC_PkgWatt);
450520de0dabSAntti Laakso 		break;
4506af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4507af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
45081b439f01SLen Brown 		do_rapl =
45091b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
45101b439f01SLen Brown 		    | RAPL_GFX | RAPL_PKG_POWER_INFO;
4511812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4512812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4513812db3f7SLen Brown 		if (rapl_joules) {
4514812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4515812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4516812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
451781824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
4518812db3f7SLen Brown 		} else {
4519812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4520812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4521812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
452281824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
4523812db3f7SLen Brown 		}
45240b2bb692SLen Brown 		break;
4525869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
4526869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4527869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
452825368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
4529869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
45301b439f01SLen Brown 		do_rapl =
45311b439f01SLen Brown 		    RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
45321b439f01SLen Brown 		    RAPL_PKG_POWER_INFO;
4533812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4534812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4535812db3f7SLen Brown 		if (rapl_joules) {
4536812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4537812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4538812db3f7SLen Brown 		} else {
4539812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4540812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4541812db3f7SLen Brown 		}
4542e6f9bb3cSLen Brown 		break;
4543869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4544869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
45451b439f01SLen Brown 		do_rapl =
45461b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS |
45471b439f01SLen Brown 		    RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
4548812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4549812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4550812db3f7SLen Brown 		if (rapl_joules) {
4551812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4552812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4553812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4554812db3f7SLen Brown 		} else {
4555812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4556812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4557812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4558812db3f7SLen Brown 		}
4559144b44b1SLen Brown 		break;
4560f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
45615ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4562144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
4563812db3f7SLen Brown 		if (rapl_joules) {
4564812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4565812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4566812db3f7SLen Brown 		} else {
4567812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4568812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4569812db3f7SLen Brown 		}
4570889facbeSLen Brown 		break;
45715ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
45721b439f01SLen Brown 		do_rapl =
45731b439f01SLen Brown 		    RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
45741b439f01SLen Brown 		    RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4575812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4576812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4577812db3f7SLen Brown 		if (rapl_joules) {
4578812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4579812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4580812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4581812db3f7SLen Brown 		} else {
4582812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4583812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4584812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4585812db3f7SLen Brown 		}
45860f644909SJacob Pan 		break;
4587889facbeSLen Brown 	default:
4588889facbeSLen Brown 		return;
4589889facbeSLen Brown 	}
4590889facbeSLen Brown 
4591889facbeSLen Brown 	/* units on package 0, verify later other packages match */
45927ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4593889facbeSLen Brown 		return;
4594889facbeSLen Brown 
4595889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
4596f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
4597144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4598144b44b1SLen Brown 	else
4599889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4600889facbeSLen Brown 
460140ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
460240ee8e3bSAndrey Semin 
4603144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
4604144b44b1SLen Brown 	if (time_unit == 0)
4605144b44b1SLen Brown 		time_unit = 0xA;
4606889facbeSLen Brown 
4607144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
4608144b44b1SLen Brown 
46099392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
4610889facbeSLen Brown 
4611889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
461296e47158SLen Brown 	if (!quiet)
4613b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
46149392bd98SCalvin Walton }
4615889facbeSLen Brown 
46169392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
46179392bd98SCalvin Walton {
46189392bd98SCalvin Walton 	unsigned long long msr;
46199392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
46209392bd98SCalvin Walton 	unsigned int has_rapl = 0;
46219392bd98SCalvin Walton 	double tdp;
46229392bd98SCalvin Walton 
46239392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
46249392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
462533eb8225SKim Phillips 		/* RAPL (Fam 17h+) */
46269392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
46279392bd98SCalvin Walton 	}
46289392bd98SCalvin Walton 
462933eb8225SKim Phillips 	if (!has_rapl || family < 0x17)
4630889facbeSLen Brown 		return;
46319392bd98SCalvin Walton 
46329392bd98SCalvin Walton 	do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
46333316f99aSCalvin Walton 	if (rapl_joules) {
46343316f99aSCalvin Walton 		BIC_PRESENT(BIC_Pkg_J);
46359392bd98SCalvin Walton 		BIC_PRESENT(BIC_Cor_J);
46363316f99aSCalvin Walton 	} else {
46373316f99aSCalvin Walton 		BIC_PRESENT(BIC_PkgWatt);
46389392bd98SCalvin Walton 		BIC_PRESENT(BIC_CorWatt);
46393316f99aSCalvin Walton 	}
46409392bd98SCalvin Walton 
46419392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
46429392bd98SCalvin Walton 		return;
46439392bd98SCalvin Walton 
46449392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
46459392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
46469392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
46479392bd98SCalvin Walton 
46489cfa8e04SPu Wen 	tdp = get_tdp_amd(family);
46499392bd98SCalvin Walton 
46509392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
46519392bd98SCalvin Walton 	if (!quiet)
46529392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
46539392bd98SCalvin Walton }
46549392bd98SCalvin Walton 
46559392bd98SCalvin Walton /*
46569392bd98SCalvin Walton  * rapl_probe()
46579392bd98SCalvin Walton  *
46589392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
46599392bd98SCalvin Walton  */
46609392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
46619392bd98SCalvin Walton {
46629392bd98SCalvin Walton 	if (genuine_intel)
46639392bd98SCalvin Walton 		rapl_probe_intel(family, model);
4664c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine)
46659392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4666889facbeSLen Brown }
4667889facbeSLen Brown 
46681b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
46693a9a941dSLen Brown {
46703a9a941dSLen Brown 	if (!genuine_intel)
46713a9a941dSLen Brown 		return;
46723a9a941dSLen Brown 
46733a9a941dSLen Brown 	if (family != 6)
46743a9a941dSLen Brown 		return;
46753a9a941dSLen Brown 
46763a9a941dSLen Brown 	switch (model) {
4677c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
467877e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
46795e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
46803a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
4681869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
46823a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
46833a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
46843a9a941dSLen Brown 	default:
46853a9a941dSLen Brown 		return;
46863a9a941dSLen Brown 	}
46873a9a941dSLen Brown }
46883a9a941dSLen Brown 
4689ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4690ac980e13SArtem Bityutskiy {
46911b439f01SLen Brown 	if (is_skx(family, model) || is_bdx(family, model) || is_icx(family, model))
4692ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4693ac980e13SArtem Bityutskiy }
4694ac980e13SArtem Bityutskiy 
4695aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model)
4696aeb01e6dSChen Yu {
4697aeb01e6dSChen Yu 	if (is_icx(family, model))
4698aeb01e6dSChen Yu 		dis_cstate_prewake = 1;
4699aeb01e6dSChen Yu }
4700aeb01e6dSChen Yu 
4701889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4702889facbeSLen Brown {
4703889facbeSLen Brown 	unsigned long long msr;
4704f4896fa5SLen Brown 	unsigned int dts, dts2;
4705889facbeSLen Brown 	int cpu;
4706889facbeSLen Brown 
4707889facbeSLen Brown 	if (!(do_dts || do_ptm))
4708889facbeSLen Brown 		return 0;
4709889facbeSLen Brown 
4710889facbeSLen Brown 	cpu = t->cpu_id;
4711889facbeSLen Brown 
4712889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4713889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4714889facbeSLen Brown 		return 0;
4715889facbeSLen Brown 
4716889facbeSLen Brown 	if (cpu_migrate(cpu)) {
47173d7772eaSLen Brown 		fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4718889facbeSLen Brown 		return -1;
4719889facbeSLen Brown 	}
4720889facbeSLen Brown 
4721889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4722889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4723889facbeSLen Brown 			return 0;
4724889facbeSLen Brown 
4725889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
47261b439f01SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts);
4727889facbeSLen Brown 
4728889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4729889facbeSLen Brown 			return 0;
4730889facbeSLen Brown 
4731889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4732889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4733b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
473455279aefSZhang Rui 			cpu, msr, tj_max - dts, tj_max - dts2);
4735889facbeSLen Brown 	}
4736889facbeSLen Brown 
4737f4896fa5SLen Brown 	if (do_dts && debug) {
4738889facbeSLen Brown 		unsigned int resolution;
4739889facbeSLen Brown 
4740889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4741889facbeSLen Brown 			return 0;
4742889facbeSLen Brown 
4743889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4744889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4745b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
474655279aefSZhang Rui 			cpu, msr, tj_max - dts, resolution);
4747889facbeSLen Brown 
4748889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4749889facbeSLen Brown 			return 0;
4750889facbeSLen Brown 
4751889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4752889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4753b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
475455279aefSZhang Rui 			cpu, msr, tj_max - dts, tj_max - dts2);
4755889facbeSLen Brown 	}
4756889facbeSLen Brown 
4757889facbeSLen Brown 	return 0;
4758889facbeSLen Brown }
4759889facbeSLen Brown 
4760889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4761889facbeSLen Brown {
47626b398625SSumeet Pawnikar 	fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n",
4763889facbeSLen Brown 		cpu, label,
4764889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4765889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4766889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4767889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4768889facbeSLen Brown 
4769889facbeSLen Brown 	return;
4770889facbeSLen Brown }
4771889facbeSLen Brown 
4772889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4773889facbeSLen Brown {
4774889facbeSLen Brown 	unsigned long long msr;
47759392bd98SCalvin Walton 	const char *msr_name;
4776889facbeSLen Brown 	int cpu;
4777889facbeSLen Brown 
4778889facbeSLen Brown 	if (!do_rapl)
4779889facbeSLen Brown 		return 0;
4780889facbeSLen Brown 
4781889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4782889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4783889facbeSLen Brown 		return 0;
4784889facbeSLen Brown 
4785889facbeSLen Brown 	cpu = t->cpu_id;
4786889facbeSLen Brown 	if (cpu_migrate(cpu)) {
47873d7772eaSLen Brown 		fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4788889facbeSLen Brown 		return -1;
4789889facbeSLen Brown 	}
4790889facbeSLen Brown 
47919392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
47929392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
47939392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
47949392bd98SCalvin Walton 			return -1;
47959392bd98SCalvin Walton 	} else {
47969392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4797889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4798889facbeSLen Brown 			return -1;
47999392bd98SCalvin Walton 	}
4800889facbeSLen Brown 
48019392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4802144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
480396e47158SLen Brown 
4804144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4805144b44b1SLen Brown 
4806889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4807889facbeSLen Brown 			return -5;
4808889facbeSLen Brown 
4809b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4810889facbeSLen Brown 			cpu, msr,
4811889facbeSLen Brown 			((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4812889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4813889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4814889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4815889facbeSLen Brown 
4816144b44b1SLen Brown 	}
4817144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4818144b44b1SLen Brown 
4819889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4820889facbeSLen Brown 			return -9;
4821889facbeSLen Brown 
4822b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4823889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4824889facbeSLen Brown 
4825889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
48266b398625SSumeet Pawnikar 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n",
4827889facbeSLen Brown 			cpu,
4828889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4829889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4830889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4831889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4832f52ba931SSumeet Pawnikar 
4833f52ba931SSumeet Pawnikar 		if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr))
4834f52ba931SSumeet Pawnikar 			return -9;
4835f52ba931SSumeet Pawnikar 
4836f52ba931SSumeet Pawnikar 		fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr);
4837f52ba931SSumeet Pawnikar 		fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n",
4838*164d7a96SLen Brown 			cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN");
4839889facbeSLen Brown 	}
4840889facbeSLen Brown 
48410b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4842889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4843889facbeSLen Brown 			return -6;
4844889facbeSLen Brown 
4845b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4846889facbeSLen Brown 			cpu, msr,
4847889facbeSLen Brown 			((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4848889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4849889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4850889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
48510b2bb692SLen Brown 	}
48520b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4853889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4854889facbeSLen Brown 			return -9;
4855b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4856889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4857889facbeSLen Brown 
4858889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
4859889facbeSLen Brown 	}
4860144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
4861889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
4862889facbeSLen Brown 			return -7;
4863889facbeSLen Brown 
4864b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
4865144b44b1SLen Brown 	}
48669148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
4867889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
4868889facbeSLen Brown 			return -9;
4869b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
4870889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4871889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
4872889facbeSLen Brown 	}
4873889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
4874889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
4875889facbeSLen Brown 			return -8;
4876889facbeSLen Brown 
4877b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
4878889facbeSLen Brown 
4879889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
4880889facbeSLen Brown 			return -9;
4881b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
4882889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4883889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
4884889facbeSLen Brown 	}
4885889facbeSLen Brown 	return 0;
4886889facbeSLen Brown }
4887889facbeSLen Brown 
4888d7899447SLen Brown /*
4889d7899447SLen Brown  * SNB adds support for additional MSRs:
4890d7899447SLen Brown  *
4891d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
4892d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
4893d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
4894d7899447SLen Brown  */
4895103a8feaSLen Brown 
4896d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
4897103a8feaSLen Brown {
4898103a8feaSLen Brown 	if (!genuine_intel)
4899103a8feaSLen Brown 		return 0;
4900103a8feaSLen Brown 
4901103a8feaSLen Brown 	switch (model) {
4902869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4903869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4904869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4905869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
4906c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
4907869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
490877e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
49095e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
4910c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
49115e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
4912869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4913af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4914af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
4915869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
491625368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
4917869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4918f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
49195ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
4920f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
492120de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
4922103a8feaSLen Brown 		return 1;
4923103a8feaSLen Brown 	}
4924103a8feaSLen Brown 	return 0;
4925103a8feaSLen Brown }
4926103a8feaSLen Brown 
4927d7899447SLen Brown /*
4928570992fcSLen Brown  * HSW ULT added support for C8/C9/C10 MSRs:
4929d7899447SLen Brown  *
4930d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
4931d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
4932d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
49335a63426eSLen Brown  *
49345a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
49355a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
49365a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
49375a63426eSLen Brown  *
4938d7899447SLen Brown  */
4939570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model)
4940ca58710fSKristen Carlson Accardi {
4941ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
4942ca58710fSKristen Carlson Accardi 		return 0;
4943ca58710fSKristen Carlson Accardi 
4944ca58710fSKristen Carlson Accardi 	switch (model) {
494577e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
4946c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
4947af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4948af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
4949869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4950f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4951f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
4952ca58710fSKristen Carlson Accardi 		return 1;
4953ca58710fSKristen Carlson Accardi 	}
4954ca58710fSKristen Carlson Accardi 	return 0;
4955ca58710fSKristen Carlson Accardi }
4956ca58710fSKristen Carlson Accardi 
49570b2bb692SLen Brown /*
49580b2bb692SLen Brown  * SKL adds support for additional MSRS:
49590b2bb692SLen Brown  *
49600b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
49610b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
49620b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
49630b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
49640b2bb692SLen Brown  */
49650b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
49660b2bb692SLen Brown {
49670b2bb692SLen Brown 	if (!genuine_intel)
49680b2bb692SLen Brown 		return 0;
49690b2bb692SLen Brown 
49700b2bb692SLen Brown 	switch (model) {
4971af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4972af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
49730b2bb692SLen Brown 		return 1;
49740b2bb692SLen Brown 	}
49750b2bb692SLen Brown 	return 0;
49760b2bb692SLen Brown }
49770b2bb692SLen Brown 
4978144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
4979144b44b1SLen Brown {
4980144b44b1SLen Brown 	if (!genuine_intel)
4981144b44b1SLen Brown 		return 0;
4982144b44b1SLen Brown 	switch (model) {
4983f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
49845ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4985144b44b1SLen Brown 		return 1;
4986144b44b1SLen Brown 	}
4987144b44b1SLen Brown 	return 0;
4988144b44b1SLen Brown }
4989144b44b1SLen Brown 
4990fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
4991fb5d4327SDasaratharaman Chandramouli {
4992fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
4993fb5d4327SDasaratharaman Chandramouli 		return 0;
4994fb5d4327SDasaratharaman Chandramouli 	switch (model) {
4995869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
4996fb5d4327SDasaratharaman Chandramouli 		return 1;
4997fb5d4327SDasaratharaman Chandramouli 	}
4998fb5d4327SDasaratharaman Chandramouli 	return 0;
4999fb5d4327SDasaratharaman Chandramouli }
5000fb5d4327SDasaratharaman Chandramouli 
5001997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
5002997e5395SSrinivas Pandruvada {
5003997e5395SSrinivas Pandruvada 	if (!genuine_intel)
5004997e5395SSrinivas Pandruvada 		return 0;
5005997e5395SSrinivas Pandruvada 
5006997e5395SSrinivas Pandruvada 	switch (model) {
5007af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
5008997e5395SSrinivas Pandruvada 		return 1;
5009997e5395SSrinivas Pandruvada 	}
5010997e5395SSrinivas Pandruvada 
5011997e5395SSrinivas Pandruvada 	return 0;
5012997e5395SSrinivas Pandruvada }
5013997e5395SSrinivas Pandruvada 
5014b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
5015b2b34dfeSHubert Chrzaniuk {
5016b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
5017b2b34dfeSHubert Chrzaniuk 		return 1024;
5018b2b34dfeSHubert Chrzaniuk 	return 1;
5019b2b34dfeSHubert Chrzaniuk }
5020b2b34dfeSHubert Chrzaniuk 
5021144b44b1SLen Brown #define SLM_BCLK_FREQS 5
5022144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 };
5023144b44b1SLen Brown 
5024144b44b1SLen Brown double slm_bclk(void)
5025144b44b1SLen Brown {
5026144b44b1SLen Brown 	unsigned long long msr = 3;
5027144b44b1SLen Brown 	unsigned int i;
5028144b44b1SLen Brown 	double freq;
5029144b44b1SLen Brown 
50307ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
5031b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
5032144b44b1SLen Brown 
5033144b44b1SLen Brown 	i = msr & 0xf;
5034144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
5035b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
50360a91e551SColin Ian King 		i = 3;
5037144b44b1SLen Brown 	}
5038144b44b1SLen Brown 	freq = slm_freq_table[i];
5039144b44b1SLen Brown 
504096e47158SLen Brown 	if (!quiet)
5041b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
5042144b44b1SLen Brown 
5043144b44b1SLen Brown 	return freq;
5044144b44b1SLen Brown }
5045144b44b1SLen Brown 
5046103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
5047103a8feaSLen Brown {
5048121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
5049103a8feaSLen Brown 		return 100.00;
5050144b44b1SLen Brown 	else if (is_slm(family, model))
5051144b44b1SLen Brown 		return slm_bclk();
5052103a8feaSLen Brown 	else
5053103a8feaSLen Brown 		return 133.33;
5054103a8feaSLen Brown }
5055103a8feaSLen Brown 
50567ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
50577ab5ff49SZhang Rui {
50587ab5ff49SZhang Rui 	unsigned int eax, ebx, ecx, edx;
50597ab5ff49SZhang Rui 
50607ab5ff49SZhang Rui 	if (!genuine_intel)
50617ab5ff49SZhang Rui 		return 0;
50627ab5ff49SZhang Rui 
50637ab5ff49SZhang Rui 	if (cpu_migrate(t->cpu_id)) {
50647ab5ff49SZhang Rui 		fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
50657ab5ff49SZhang Rui 		return -1;
50667ab5ff49SZhang Rui 	}
50677ab5ff49SZhang Rui 
50687ab5ff49SZhang Rui 	if (max_level < 0x1a)
50697ab5ff49SZhang Rui 		return 0;
50707ab5ff49SZhang Rui 
50717ab5ff49SZhang Rui 	__cpuid(0x1a, eax, ebx, ecx, edx);
50727ab5ff49SZhang Rui 	eax = (eax >> 24) & 0xFF;
50737ab5ff49SZhang Rui 	if (eax == 0x20)
50747ab5ff49SZhang Rui 		t->is_atom = true;
50757ab5ff49SZhang Rui 	return 0;
50767ab5ff49SZhang Rui }
50777ab5ff49SZhang Rui 
5078889facbeSLen Brown /*
5079889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
5080889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
5081889facbeSLen Brown  * This is usually equal to tjMax.
5082889facbeSLen Brown  *
5083889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
5084889facbeSLen Brown  * but also allow cmdline over-ride with -T.
5085889facbeSLen Brown  *
5086889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
5087889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
5088889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
5089889facbeSLen Brown  */
50906ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
50916ff7cb37SLen Brown {
5092b2b94be7SLen Brown 	unsigned long long msr;
509355279aefSZhang Rui 	unsigned int tcc_default, tcc_offset;
5094b2b94be7SLen Brown 	int cpu;
5095b2b94be7SLen Brown 
509655279aefSZhang Rui 	/* tj_max is used only for dts or ptm */
5097889facbeSLen Brown 	if (!(do_dts || do_ptm))
5098889facbeSLen Brown 		return 0;
5099889facbeSLen Brown 
5100889facbeSLen Brown 	/* this is a per-package concept */
5101889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5102889facbeSLen Brown 		return 0;
5103889facbeSLen Brown 
5104b2b94be7SLen Brown 	cpu = t->cpu_id;
5105b2b94be7SLen Brown 	if (cpu_migrate(cpu)) {
5106b2b94be7SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5107b2b94be7SLen Brown 		return -1;
5108b2b94be7SLen Brown 	}
5109b2b94be7SLen Brown 
511055279aefSZhang Rui 	if (tj_max_override != 0) {
511155279aefSZhang Rui 		tj_max = tj_max_override;
51121b439f01SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max);
5113889facbeSLen Brown 		return 0;
5114889facbeSLen Brown 	}
5115889facbeSLen Brown 
5116b2b94be7SLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
5117b2b94be7SLen Brown 	if (!do_nhm_platform_info)
5118b2b94be7SLen Brown 		goto guess;
5119b2b94be7SLen Brown 
5120b2b94be7SLen Brown 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5121b2b94be7SLen Brown 		goto guess;
5122b2b94be7SLen Brown 
512355279aefSZhang Rui 	tcc_default = (msr >> 16) & 0xFF;
5124b2b94be7SLen Brown 
51250b9a0b9bSZhang Rui 	if (!quiet) {
51260b9a0b9bSZhang Rui 		switch (tcc_offset_bits) {
51270b9a0b9bSZhang Rui 		case 4:
51280b9a0b9bSZhang Rui 			tcc_offset = (msr >> 24) & 0xF;
51290b9a0b9bSZhang Rui 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
513055279aefSZhang Rui 				cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
51310b9a0b9bSZhang Rui 			break;
51320b9a0b9bSZhang Rui 		case 6:
51330b9a0b9bSZhang Rui 			tcc_offset = (msr >> 24) & 0x3F;
51340b9a0b9bSZhang Rui 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
513555279aefSZhang Rui 				cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
51360b9a0b9bSZhang Rui 			break;
51370b9a0b9bSZhang Rui 		default:
51381b439f01SLen Brown 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default);
51390b9a0b9bSZhang Rui 			break;
51400b9a0b9bSZhang Rui 		}
51410b9a0b9bSZhang Rui 	}
5142b2b94be7SLen Brown 
514355279aefSZhang Rui 	if (!tcc_default)
5144b2b94be7SLen Brown 		goto guess;
5145b2b94be7SLen Brown 
514655279aefSZhang Rui 	tj_max = tcc_default;
5147b2b94be7SLen Brown 
5148889facbeSLen Brown 	return 0;
5149889facbeSLen Brown 
5150b2b94be7SLen Brown guess:
515155279aefSZhang Rui 	tj_max = TJMAX_DEFAULT;
51521b439f01SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max);
5153889facbeSLen Brown 
5154889facbeSLen Brown 	return 0;
5155889facbeSLen Brown }
515669807a63SLen Brown 
5157aa8d8cc7SLen Brown void decode_feature_control_msr(void)
5158aa8d8cc7SLen Brown {
5159aa8d8cc7SLen Brown 	unsigned long long msr;
5160aa8d8cc7SLen Brown 
5161f6505c88SSean Christopherson 	if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5162aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
51631b439f01SLen Brown 			base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : "");
5164aa8d8cc7SLen Brown }
5165aa8d8cc7SLen Brown 
516669807a63SLen Brown void decode_misc_enable_msr(void)
516769807a63SLen Brown {
516869807a63SLen Brown 	unsigned long long msr;
516969807a63SLen Brown 
5170f26b1519SLen Brown 	if (!genuine_intel)
5171f26b1519SLen Brown 		return;
5172f26b1519SLen Brown 
517369807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5174e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
517569807a63SLen Brown 			base_cpu, msr,
5176e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5177e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5178fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5179e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5180e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
518169807a63SLen Brown }
518269807a63SLen Brown 
518333148d67SLen Brown void decode_misc_feature_control(void)
518433148d67SLen Brown {
518533148d67SLen Brown 	unsigned long long msr;
518633148d67SLen Brown 
518733148d67SLen Brown 	if (!has_misc_feature_control)
518833148d67SLen Brown 		return;
518933148d67SLen Brown 
519033148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
51911b439f01SLen Brown 		fprintf(outf,
51921b439f01SLen Brown 			"cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
51931b439f01SLen Brown 			base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "",
51941b439f01SLen Brown 			msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : "");
519533148d67SLen Brown }
51961b439f01SLen Brown 
5197f0057310SLen Brown /*
5198f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
5199f0057310SLen Brown  *
5200f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
5201f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
5202f0057310SLen Brown  * bit[1] less so...
5203f0057310SLen Brown  */
5204f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
5205f0057310SLen Brown {
5206f0057310SLen Brown 	unsigned long long msr;
5207f0057310SLen Brown 
5208f0057310SLen Brown 	if (!do_nhm_platform_info)
5209f0057310SLen Brown 		return;
5210f0057310SLen Brown 
5211cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
5212cf4cbe53SLen Brown 		return;
5213cf4cbe53SLen Brown 
5214f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5215ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5216f0057310SLen Brown 			base_cpu, msr,
52171b439f01SLen Brown 			msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS");
5218f0057310SLen Brown }
52191b439f01SLen Brown 
522071616c8eSLen Brown /*
522171616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
522271616c8eSLen Brown  *
522371616c8eSLen Brown  * This MSRs are present on Silvermont processors,
522471616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
522571616c8eSLen Brown  */
522671616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
522771616c8eSLen Brown {
522871616c8eSLen Brown 	unsigned long long msr;
522971616c8eSLen Brown 
523071616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
523171616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
523271616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
523371616c8eSLen Brown 
523471616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
523571616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
523671616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
523771616c8eSLen Brown }
52387f5c258eSLen Brown 
5239f5a4c76aSLen Brown /*
5240f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
5241f5a4c76aSLen Brown  */
5242f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
5243f5a4c76aSLen Brown {
5244f5a4c76aSLen Brown 
5245f5a4c76aSLen Brown 	switch (model) {
5246f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5247f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5248f5a4c76aSLen Brown 	case 0x1F:		/* Core i7 and i5 Processor - Nehalem */
5249f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
5250f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
5251f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
5252f5a4c76aSLen Brown 
5253f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
5254f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
5255f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
5256f5a4c76aSLen Brown 
5257f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
5258f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
5259f5a4c76aSLen Brown 
5260f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
52615ebb34edSPeter Zijlstra 	case INTEL_FAM6_BROADWELL_D:	/* BDX-DE */
5262f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
5263f5a4c76aSLen Brown 
5264af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:
5265c66f78a6SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE:
5266af239c44SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE_L:
5267c66f78a6SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE:
5268081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE_L:
5269081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE:
5270af239c44SPeter Zijlstra 		return INTEL_FAM6_SKYLAKE_L;
5271937807d3SLen Brown 
5272af239c44SPeter Zijlstra 	case INTEL_FAM6_ICELAKE_L:
5273d93ea567SRajneesh Bhardwaj 	case INTEL_FAM6_ICELAKE_NNPI:
52744bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE_L:
52754bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE:
5276e7af1ed3SLen Brown 	case INTEL_FAM6_ROCKETLAKE:
5277e7af1ed3SLen Brown 	case INTEL_FAM6_LAKEFIELD:
5278e7af1ed3SLen Brown 	case INTEL_FAM6_ALDERLAKE:
52795683460bSChen Yu 	case INTEL_FAM6_ALDERLAKE_L:
5280af239c44SPeter Zijlstra 		return INTEL_FAM6_CANNONLAKE_L;
5281b62b3184SZhang Rui 
5282d7814c30SChen Yu 	case INTEL_FAM6_ATOM_TREMONT_L:
5283d7814c30SChen Yu 		return INTEL_FAM6_ATOM_TREMONT;
5284d7814c30SChen Yu 
52856c5c6560SChen Yu 	case INTEL_FAM6_ICELAKE_D:
5286e7af1ed3SLen Brown 	case INTEL_FAM6_SAPPHIRERAPIDS_X:
528725368d7cSChen Yu 		return INTEL_FAM6_ICELAKE_X;
5288f5a4c76aSLen Brown 	}
5289f5a4c76aSLen Brown 	return model;
5290f5a4c76aSLen Brown }
5291d76bb7a0SLen Brown 
5292d76bb7a0SLen Brown void print_dev_latency(void)
5293d76bb7a0SLen Brown {
5294d76bb7a0SLen Brown 	char *path = "/dev/cpu_dma_latency";
5295d76bb7a0SLen Brown 	int fd;
5296d76bb7a0SLen Brown 	int value;
5297d76bb7a0SLen Brown 	int retval;
5298d76bb7a0SLen Brown 
5299d76bb7a0SLen Brown 	fd = open(path, O_RDONLY);
5300d76bb7a0SLen Brown 	if (fd < 0) {
5301d76bb7a0SLen Brown 		warn("fopen %s\n", path);
5302d76bb7a0SLen Brown 		return;
5303d76bb7a0SLen Brown 	}
5304d76bb7a0SLen Brown 
5305d76bb7a0SLen Brown 	retval = read(fd, (void *)&value, sizeof(int));
5306d76bb7a0SLen Brown 	if (retval != sizeof(int)) {
5307d76bb7a0SLen Brown 		warn("read %s\n", path);
5308d76bb7a0SLen Brown 		close(fd);
5309d76bb7a0SLen Brown 		return;
5310d76bb7a0SLen Brown 	}
53111b439f01SLen Brown 	fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained");
5312d76bb7a0SLen Brown 
5313d76bb7a0SLen Brown 	close(fd);
5314d76bb7a0SLen Brown }
5315d76bb7a0SLen Brown 
53162af4f9b8SLen Brown /*
53172af4f9b8SLen Brown  * Linux-perf manages the the HW instructions-retired counter
53182af4f9b8SLen Brown  * by enabling when requested, and hiding rollover
53192af4f9b8SLen Brown  */
53202af4f9b8SLen Brown void linux_perf_init(void)
53212af4f9b8SLen Brown {
53222af4f9b8SLen Brown 	if (!BIC_IS_ENABLED(BIC_IPC))
53232af4f9b8SLen Brown 		return;
53242af4f9b8SLen Brown 
53252af4f9b8SLen Brown 	if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
53262af4f9b8SLen Brown 		return;
53272af4f9b8SLen Brown 
53282af4f9b8SLen Brown 	fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
53292af4f9b8SLen Brown 	if (fd_instr_count_percpu == NULL)
53302af4f9b8SLen Brown 		err(-1, "calloc fd_instr_count_percpu");
53312af4f9b8SLen Brown 
53322af4f9b8SLen Brown 	BIC_PRESENT(BIC_IPC);
53332af4f9b8SLen Brown }
53342af4f9b8SLen Brown 
5335fcd17211SLen Brown void process_cpuid()
5336103a8feaSLen Brown {
533734041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
533834041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5339b3a34e93SLen Brown 	unsigned int has_turbo;
5340ed0757b8SLen Brown 	unsigned long long ucode_patch = 0;
5341103a8feaSLen Brown 
5342103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
5343103a8feaSLen Brown 
53445aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
5345103a8feaSLen Brown 
534634041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5347103a8feaSLen Brown 		genuine_intel = 1;
534834041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
534934041551SLen Brown 		authentic_amd = 1;
5350c1c10cc7SPu Wen 	else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5351c1c10cc7SPu Wen 		hygon_genuine = 1;
5352103a8feaSLen Brown 
535396e47158SLen Brown 	if (!quiet)
5354ed0757b8SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5355ed0757b8SLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5356103a8feaSLen Brown 
53575aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
5358103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
5359103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
5360103a8feaSLen Brown 	stepping = fms & 0xf;
53615aa3d1a2SCalvin Walton 	if (family == 0xf)
53625aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
53635aa3d1a2SCalvin Walton 	if (family >= 6)
5364103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
536534041551SLen Brown 	ecx_flags = ecx;
536634041551SLen Brown 	edx_flags = edx;
5367103a8feaSLen Brown 
5368ed0757b8SLen Brown 	if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5369ed0757b8SLen Brown 		warnx("get_msr(UCODE)\n");
5370ed0757b8SLen Brown 
5371103a8feaSLen Brown 	/*
5372103a8feaSLen Brown 	 * check max extended function levels of CPUID.
5373103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
5374103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
5375103a8feaSLen Brown 	 */
5376103a8feaSLen Brown 	ebx = ecx = edx = 0;
53775aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5378103a8feaSLen Brown 
537934041551SLen Brown 	if (!quiet) {
5380ed0757b8SLen Brown 		fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
53811b439f01SLen Brown 			family, model, stepping, family, model, stepping,
53821b439f01SLen Brown 			(unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5383ed0757b8SLen Brown 		fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
538434041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
538534041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
538634041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
538734041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
538834041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
538934041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
539034041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
539134041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
539234041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
53931b439f01SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-");
539434041551SLen Brown 	}
539525368d7cSChen Yu 	if (genuine_intel) {
5396e9d3092fSZhang Rui 		model_orig = model;
5397f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
539825368d7cSChen Yu 	}
539934041551SLen Brown 
540034041551SLen Brown 	if (!(edx_flags & (1 << 5)))
540134041551SLen Brown 		errx(1, "CPUID: no MSR");
540234041551SLen Brown 
540361a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
5404103a8feaSLen Brown 
5405103a8feaSLen Brown 		/*
5406103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5407103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
5408103a8feaSLen Brown 		 */
54095aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
54108209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
5411d7899447SLen Brown 	}
5412103a8feaSLen Brown 
5413103a8feaSLen Brown 	/*
5414103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5415103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
5416103a8feaSLen Brown 	 */
5417103a8feaSLen Brown 
54185aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
54198209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
5420812db3f7SLen Brown 	if (has_aperf) {
5421812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
5422812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
5423812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
5424812db3f7SLen Brown 	}
5425889facbeSLen Brown 	do_dts = eax & (1 << 0);
5426812db3f7SLen Brown 	if (do_dts)
5427812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
5428b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
5429889facbeSLen Brown 	do_ptm = eax & (1 << 6);
5430812db3f7SLen Brown 	if (do_ptm)
5431812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
54327f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
54337f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
54347f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
54357f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
54367f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
5437889facbeSLen Brown 	has_epb = ecx & (1 << 3);
5438889facbeSLen Brown 
543996e47158SLen Brown 	if (!quiet)
5440b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
54417f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
54427f5c258eSLen Brown 			has_aperf ? "" : "No-",
5443b3a34e93SLen Brown 			has_turbo ? "" : "No-",
54447f5c258eSLen Brown 			do_dts ? "" : "No-",
54457f5c258eSLen Brown 			do_ptm ? "" : "No-",
54467f5c258eSLen Brown 			has_hwp ? "" : "No-",
54477f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
54487f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
54491b439f01SLen Brown 			has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-");
5450103a8feaSLen Brown 
545196e47158SLen Brown 	if (!quiet)
545269807a63SLen Brown 		decode_misc_enable_msr();
545369807a63SLen Brown 
545496e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
5455aa8d8cc7SLen Brown 		int has_sgx;
5456aa8d8cc7SLen Brown 
5457aa8d8cc7SLen Brown 		ecx = 0;
5458aa8d8cc7SLen Brown 
5459aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5460aa8d8cc7SLen Brown 
5461aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
5462aa8d8cc7SLen Brown 		fprintf(outf, "CPUID(7): %sSGX\n", has_sgx ? "" : "No-");
5463aa8d8cc7SLen Brown 
5464aa8d8cc7SLen Brown 		if (has_sgx)
5465aa8d8cc7SLen Brown 			decode_feature_control_msr();
5466aa8d8cc7SLen Brown 	}
5467aa8d8cc7SLen Brown 
546861a87ba7SLen Brown 	if (max_level >= 0x15) {
54698a5bdf41SLen Brown 		unsigned int eax_crystal;
54708a5bdf41SLen Brown 		unsigned int ebx_tsc;
54718a5bdf41SLen Brown 
54728a5bdf41SLen Brown 		/*
54738a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
54748a5bdf41SLen Brown 		 */
54758a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
54765aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
54778a5bdf41SLen Brown 
54788a5bdf41SLen Brown 		if (ebx_tsc != 0) {
54798a5bdf41SLen Brown 
548096e47158SLen Brown 			if (!quiet && (ebx != 0))
5481b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
54828a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
54838a5bdf41SLen Brown 
54848a5bdf41SLen Brown 			if (crystal_hz == 0)
54858a5bdf41SLen Brown 				switch (model) {
5486af239c44SPeter Zijlstra 				case INTEL_FAM6_SKYLAKE_L:	/* SKL */
5487e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
5488e8efbc80SLen Brown 					break;
54895ebb34edSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
5490ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
5491ec53e594SLen Brown 					break;
5492869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
5493f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5494e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
54958a5bdf41SLen Brown 					break;
54968a5bdf41SLen Brown 				default:
54978a5bdf41SLen Brown 					crystal_hz = 0;
54988a5bdf41SLen Brown 				}
54998a5bdf41SLen Brown 
55008a5bdf41SLen Brown 			if (crystal_hz) {
55018a5bdf41SLen Brown 				tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal;
550296e47158SLen Brown 				if (!quiet)
5503b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
55048a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
55058a5bdf41SLen Brown 			}
55068a5bdf41SLen Brown 		}
55078a5bdf41SLen Brown 	}
550861a87ba7SLen Brown 	if (max_level >= 0x16) {
550961a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
551061a87ba7SLen Brown 
551161a87ba7SLen Brown 		/*
551261a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
551361a87ba7SLen Brown 		 */
551461a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
551561a87ba7SLen Brown 
55165aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
551796e47158SLen Brown 		if (!quiet)
5518b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
551961a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
552061a87ba7SLen Brown 	}
55218a5bdf41SLen Brown 
5522b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
5523b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5524b2b34dfeSHubert Chrzaniuk 
5525812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
5526812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
5527812db3f7SLen Brown 
5528812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
5529812db3f7SLen Brown 		do_nhm_platform_info = 1;
5530812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
5531812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
5532812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
5533812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
5534812db3f7SLen Brown 	}
5535d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
5536812db3f7SLen Brown 
5537812db3f7SLen Brown 	if (do_snb_cstates)
5538812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
5539812db3f7SLen Brown 
55405a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
55410f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
55420f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
55430f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
55440f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
55450f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
55460f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
55470f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
55480f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
55490539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
55500f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
55510f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
55520f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
55530f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
55540539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
55550539ba11SLen Brown 		use_c1_residency_msr = 1;
55560539ba11SLen Brown 	}
555720de0dabSAntti Laakso 	if (is_jvl(family, model)) {
555820de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_CPU_c3);
555920de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_CPU_c7);
556020de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc2);
556120de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc3);
556220de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc6);
556320de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc7);
556420de0dabSAntti Laakso 	}
55657170a374SLen Brown 	if (is_dnv(family, model)) {
55667170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
55677170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
55687170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
55697170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
55707170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
55717170a374SLen Brown 		use_c1_residency_msr = 1;
55727170a374SLen Brown 	}
557325368d7cSChen Yu 	if (is_skx(family, model) || is_icx(family, model)) {
557434c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
557534c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
557634c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
557734c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
557834c76197SLen Brown 	}
5579ade0ebacSLen Brown 	if (is_bdx(family, model)) {
5580ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
5581ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
5582ade0ebacSLen Brown 	}
5583570992fcSLen Brown 	if (has_c8910_msrs(family, model)) {
5584c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL__8)
55850f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc8);
5586c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL__9)
55870f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc9);
5588c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL_10)
55890f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc10);
55900f47c08dSLen Brown 	}
5591570992fcSLen Brown 	do_irtl_hsw = has_c8910_msrs(family, model);
5592a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
5593a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
5594a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
5595a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
5596a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
5597a99d8730SLen Brown 	}
5598144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
5599fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates = is_knl(family, model);
5600103a8feaSLen Brown 
56011b439f01SLen Brown 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model))
5602562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
5603103a8feaSLen Brown 
560496e47158SLen Brown 	if (!quiet)
5605f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
5606f0057310SLen Brown 
560796e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
560871616c8eSLen Brown 		decode_c6_demotion_policy_msr();
560971616c8eSLen Brown 
5610889facbeSLen Brown 	rapl_probe(family, model);
56113a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
5612ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
5613889facbeSLen Brown 
56140b9a0b9bSZhang Rui 	check_tcc_offset(model_orig);
56150b9a0b9bSZhang Rui 
561696e47158SLen Brown 	if (!quiet)
56171b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
5618fcd17211SLen Brown 
561941618e63SLen Brown 	if (!quiet)
5620d76bb7a0SLen Brown 		print_dev_latency();
5621d76bb7a0SLen Brown 	if (!quiet)
562241618e63SLen Brown 		dump_sysfs_cstate_config();
56237293fccdSLen Brown 	if (!quiet)
56247293fccdSLen Brown 		dump_sysfs_pstate_config();
562541618e63SLen Brown 
56268c69da29SChen Yu 	if (has_skl_msrs(family, model) || is_ehl(family, model))
5627a2b7b749SLen Brown 		calculate_tsc_tweak();
5628a2b7b749SLen Brown 
5629812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5630812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
5631fdf676e5SLen Brown 
5632812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5633812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
563427d47356SLen Brown 
5635b4b91569SRafael Antognolli 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5636b4b91569SRafael Antognolli 		BIC_PRESENT(BIC_GFXACTMHz);
5637b4b91569SRafael Antognolli 
5638be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5639be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
5640be0e54c4SLen Brown 	else
5641be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
5642be0e54c4SLen Brown 
5643eae97e05SChen Yu 	if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK))
5644eae97e05SChen Yu 		BIC_PRESENT(BIC_CORE_THROT_CNT);
5645eae97e05SChen Yu 	else
5646eae97e05SChen Yu 		BIC_NOT_PRESENT(BIC_CORE_THROT_CNT);
5647eae97e05SChen Yu 
56481f81c5efSLen Brown 	if (!access(sys_lpi_file_sysfs, R_OK)) {
56491f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_sysfs;
5650be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
56511f81c5efSLen Brown 	} else if (!access(sys_lpi_file_debugfs, R_OK)) {
56521f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_debugfs;
56531f81c5efSLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
56541f81c5efSLen Brown 	} else {
56551f81c5efSLen Brown 		sys_lpi_file_sysfs = NULL;
5656be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
56571f81c5efSLen Brown 	}
5658be0e54c4SLen Brown 
565996e47158SLen Brown 	if (!quiet)
566033148d67SLen Brown 		decode_misc_feature_control();
566133148d67SLen Brown 
5662889facbeSLen Brown 	return;
5663103a8feaSLen Brown }
5664103a8feaSLen Brown 
5665103a8feaSLen Brown /*
5666103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
5667103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
5668103a8feaSLen Brown  */
5669103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
5670103a8feaSLen Brown {
5671103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
5672103a8feaSLen Brown 		return 1;
5673103a8feaSLen Brown 	else
5674103a8feaSLen Brown 		return 0;
5675103a8feaSLen Brown }
5676103a8feaSLen Brown 
5677103a8feaSLen Brown int open_dev_cpu_msr(int dummy1)
5678103a8feaSLen Brown {
5679103a8feaSLen Brown 	return 0;
5680103a8feaSLen Brown }
5681103a8feaSLen Brown 
5682c98d5d94SLen Brown void topology_probe()
5683c98d5d94SLen Brown {
5684c98d5d94SLen Brown 	int i;
5685c98d5d94SLen Brown 	int max_core_id = 0;
5686c98d5d94SLen Brown 	int max_package_id = 0;
56876de68fe1SLen Brown 	int max_die_id = 0;
5688c98d5d94SLen Brown 	int max_siblings = 0;
5689c98d5d94SLen Brown 
5690c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
5691843c5791SPrarit Bhargava 	set_max_cpu_num();
5692c98d5d94SLen Brown 	topo.num_cpus = 0;
5693c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
5694c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
5695812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
5696c98d5d94SLen Brown 
5697d8af6f5fSLen Brown 	if (debug > 1)
5698b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5699c98d5d94SLen Brown 
5700c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5701b2c95d90SJosh Triplett 	if (cpus == NULL)
5702b2c95d90SJosh Triplett 		err(1, "calloc cpus");
5703c98d5d94SLen Brown 
5704c98d5d94SLen Brown 	/*
5705c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
5706c98d5d94SLen Brown 	 */
5707c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5708b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
5709b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5710c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5711c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5712c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
5713c98d5d94SLen Brown 
5714c98d5d94SLen Brown 	/*
57151ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
57161ef7d21aSLen Brown 	 */
57171ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
57181ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
57191ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
57201ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
57211ef7d21aSLen Brown 	}
57221ef7d21aSLen Brown 
57231ef7d21aSLen Brown 	/*
5724c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
5725c98d5d94SLen Brown 	 */
5726c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5727b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
5728b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5729c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5730c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5731c98d5d94SLen Brown 
57328cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
5733c98d5d94SLen Brown 
5734c98d5d94SLen Brown 	/*
5735c98d5d94SLen Brown 	 * For online cpus
5736c98d5d94SLen Brown 	 * find max_core_id, max_package_id
5737c98d5d94SLen Brown 	 */
5738c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
5739c98d5d94SLen Brown 		int siblings;
5740c98d5d94SLen Brown 
5741c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
5742d8af6f5fSLen Brown 			if (debug > 1)
5743b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
5744c98d5d94SLen Brown 			continue;
5745c98d5d94SLen Brown 		}
5746c98d5d94SLen Brown 
57470e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
57480e2d8f05SLen Brown 
57490e2d8f05SLen Brown 		/* get package information */
5750c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
5751c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
5752c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
5753c98d5d94SLen Brown 
57546de68fe1SLen Brown 		/* get die information */
57556de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
57566de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
57576de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
57586de68fe1SLen Brown 
57590e2d8f05SLen Brown 		/* get numa node information */
5760ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5761ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
5762ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
57630e2d8f05SLen Brown 
57640e2d8f05SLen Brown 		/* get core information */
57650e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
57660e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
57670e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
57680e2d8f05SLen Brown 
57690e2d8f05SLen Brown 		/* get thread information */
57700e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
5771c98d5d94SLen Brown 		if (siblings > max_siblings)
5772c98d5d94SLen Brown 			max_siblings = siblings;
57734f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
57748cb48b32SPrarit Bhargava 			topo.num_cores++;
5775c98d5d94SLen Brown 	}
5776ef605741SPrarit Bhargava 
577770a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
5778d8af6f5fSLen Brown 	if (debug > 1)
57791b439f01SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
578070a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5781812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5782c98d5d94SLen Brown 
57836de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
57846de68fe1SLen Brown 	if (debug > 1)
57851b439f01SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die);
57866de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
57876de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
57886de68fe1SLen Brown 
5789c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5790d8af6f5fSLen Brown 	if (debug > 1)
57911b439f01SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages);
57927da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5793812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5794c98d5d94SLen Brown 
5795ef605741SPrarit Bhargava 	set_node_data();
5796ef605741SPrarit Bhargava 	if (debug > 1)
579770a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
579801235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
579901235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5800ef605741SPrarit Bhargava 
580170a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5802d8af6f5fSLen Brown 	if (debug > 1)
5803b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
58042ffbb224SPrarit Bhargava 
58052ffbb224SPrarit Bhargava 	if (debug < 1)
58062ffbb224SPrarit Bhargava 		return;
58072ffbb224SPrarit Bhargava 
58082ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
58090ec712e3SLen Brown 		if (cpu_is_not_present(i))
58100ec712e3SLen Brown 			continue;
58112ffbb224SPrarit Bhargava 		fprintf(outf,
58126de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
58136de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
58141b439f01SLen Brown 			cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id);
58152ffbb224SPrarit Bhargava 	}
58162ffbb224SPrarit Bhargava 
5817c98d5d94SLen Brown }
5818c98d5d94SLen Brown 
58191b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
5820c98d5d94SLen Brown {
5821c98d5d94SLen Brown 	int i;
58221b439f01SLen Brown 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
582340f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5824c98d5d94SLen Brown 
582540f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5826c98d5d94SLen Brown 	if (*t == NULL)
5827c98d5d94SLen Brown 		goto error;
5828c98d5d94SLen Brown 
582940f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
5830c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
5831c98d5d94SLen Brown 
583240f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
5833c98d5d94SLen Brown 	if (*c == NULL)
5834c98d5d94SLen Brown 		goto error;
5835c98d5d94SLen Brown 
583640f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
5837c98d5d94SLen Brown 		(*c)[i].core_id = -1;
5838c98d5d94SLen Brown 
5839678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
5840c98d5d94SLen Brown 	if (*p == NULL)
5841c98d5d94SLen Brown 		goto error;
5842c98d5d94SLen Brown 
5843c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
5844c98d5d94SLen Brown 		(*p)[i].package_id = i;
5845c98d5d94SLen Brown 
5846c98d5d94SLen Brown 	return;
5847c98d5d94SLen Brown error:
5848b2c95d90SJosh Triplett 	err(1, "calloc counters");
5849c98d5d94SLen Brown }
58501b439f01SLen Brown 
5851c98d5d94SLen Brown /*
5852c98d5d94SLen Brown  * init_counter()
5853c98d5d94SLen Brown  *
5854c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
5855c98d5d94SLen Brown  */
58561b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id)
5857c98d5d94SLen Brown {
58588cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
585940f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
58608cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
58618cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
5862c98d5d94SLen Brown 	struct thread_data *t;
5863c98d5d94SLen Brown 	struct core_data *c;
5864c98d5d94SLen Brown 	struct pkg_data *p;
5865c98d5d94SLen Brown 
586642dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
586742dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
586842dd4520SNathan Ciobanu 	 */
586942dd4520SNathan Ciobanu 	if (node_id < 0)
587042dd4520SNathan Ciobanu 		node_id = 0;
587142dd4520SNathan Ciobanu 
587240f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
587340f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
58748cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
5875c98d5d94SLen Brown 
5876c98d5d94SLen Brown 	t->cpu_id = cpu_id;
58778cb48b32SPrarit Bhargava 	if (thread_id == 0) {
5878c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
5879c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
5880c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
5881c98d5d94SLen Brown 	}
5882c98d5d94SLen Brown 
58838cb48b32SPrarit Bhargava 	c->core_id = core_id;
58848cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
5885c98d5d94SLen Brown }
5886c98d5d94SLen Brown 
5887c98d5d94SLen Brown int initialize_counters(int cpu_id)
5888c98d5d94SLen Brown {
58898cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
58908cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
5891c98d5d94SLen Brown 	return 0;
5892c98d5d94SLen Brown }
5893c98d5d94SLen Brown 
5894c98d5d94SLen Brown void allocate_output_buffer()
5895c98d5d94SLen Brown {
5896eeb71c95SNaoya Horiguchi 	output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
5897c98d5d94SLen Brown 	outp = output_buffer;
5898b2c95d90SJosh Triplett 	if (outp == NULL)
5899b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
5900c98d5d94SLen Brown }
59011b439f01SLen Brown 
590236229897SLen Brown void allocate_fd_percpu(void)
590336229897SLen Brown {
590401a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
590536229897SLen Brown 	if (fd_percpu == NULL)
590636229897SLen Brown 		err(-1, "calloc fd_percpu");
590736229897SLen Brown }
59081b439f01SLen Brown 
5909562a2d37SLen Brown void allocate_irq_buffers(void)
5910562a2d37SLen Brown {
5911562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
5912562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
5913562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
5914c98d5d94SLen Brown 
591501a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
5916562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
591701a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
5918562a2d37SLen Brown }
59191b439f01SLen Brown 
5920c98d5d94SLen Brown void setup_all_buffers(void)
5921c98d5d94SLen Brown {
5922c98d5d94SLen Brown 	topology_probe();
5923562a2d37SLen Brown 	allocate_irq_buffers();
592436229897SLen Brown 	allocate_fd_percpu();
5925c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
5926c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
5927c98d5d94SLen Brown 	allocate_output_buffer();
5928c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
5929c98d5d94SLen Brown }
59303b4d5c7fSAndy Shevchenko 
59317ce7d5deSPrarit Bhargava void set_base_cpu(void)
59327ce7d5deSPrarit Bhargava {
59337ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
59347ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
59357ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
59367ce7d5deSPrarit Bhargava 
59377ce7d5deSPrarit Bhargava 	if (debug > 1)
5938b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
59397ce7d5deSPrarit Bhargava }
59407ce7d5deSPrarit Bhargava 
5941103a8feaSLen Brown void turbostat_init()
5942103a8feaSLen Brown {
59437ce7d5deSPrarit Bhargava 	setup_all_buffers();
59447ce7d5deSPrarit Bhargava 	set_base_cpu();
5945103a8feaSLen Brown 	check_dev_msr();
594698481e79SLen Brown 	check_permissions();
5947fcd17211SLen Brown 	process_cpuid();
59482af4f9b8SLen Brown 	linux_perf_init();
5949103a8feaSLen Brown 
595096e47158SLen Brown 	if (!quiet)
59517f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
59527f5c258eSLen Brown 
595396e47158SLen Brown 	if (!quiet)
5954889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
5955889facbeSLen Brown 
595696e47158SLen Brown 	if (!quiet)
59573a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
59583a9a941dSLen Brown 
595996e47158SLen Brown 	if (!quiet)
5960889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
5961889facbeSLen Brown 
5962889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
5963889facbeSLen Brown 
59647ab5ff49SZhang Rui 	for_all_cpus(get_cpu_type, ODD_COUNTERS);
59657ab5ff49SZhang Rui 	for_all_cpus(get_cpu_type, EVEN_COUNTERS);
59667ab5ff49SZhang Rui 
596796e47158SLen Brown 	if (!quiet)
5968889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
59695a63426eSLen Brown 
597096e47158SLen Brown 	if (!quiet && do_irtl_snb)
59715a63426eSLen Brown 		print_irtl();
5972*164d7a96SLen Brown 
5973*164d7a96SLen Brown 	if (DO_BIC(BIC_IPC))
5974*164d7a96SLen Brown 		(void)get_instr_count_fd(base_cpu);
5975103a8feaSLen Brown }
5976103a8feaSLen Brown 
5977103a8feaSLen Brown int fork_it(char **argv)
5978103a8feaSLen Brown {
5979103a8feaSLen Brown 	pid_t child_pid;
5980d91bb17cSLen Brown 	int status;
5981d15cf7c1SLen Brown 
5982218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
5983d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
59844c2122d4SLen Brown 	first_counter_read = 0;
5985d91bb17cSLen Brown 	if (status)
5986d91bb17cSLen Brown 		exit(status);
5987d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
5988d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
5989103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
5990103a8feaSLen Brown 
5991103a8feaSLen Brown 	child_pid = fork();
5992103a8feaSLen Brown 	if (!child_pid) {
5993103a8feaSLen Brown 		/* child */
5994103a8feaSLen Brown 		execvp(argv[0], argv);
59950815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
5996103a8feaSLen Brown 	} else {
5997103a8feaSLen Brown 
5998103a8feaSLen Brown 		/* parent */
5999b2c95d90SJosh Triplett 		if (child_pid == -1)
6000b2c95d90SJosh Triplett 			err(1, "fork");
6001103a8feaSLen Brown 
6002103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
6003103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
6004b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
6005b2c95d90SJosh Triplett 			err(status, "waitpid");
60062a954966SDavid Arcari 
60072a954966SDavid Arcari 		if (WIFEXITED(status))
60082a954966SDavid Arcari 			status = WEXITSTATUS(status);
6009103a8feaSLen Brown 	}
6010c98d5d94SLen Brown 	/*
6011c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
6012c98d5d94SLen Brown 	 * because re-starting is problematic when forking
6013c98d5d94SLen Brown 	 */
6014218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
6015c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
6016103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
6017103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
6018ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
6019ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
6020ba3dec99SLen Brown 	else {
6021c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
6022c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
6023ba3dec99SLen Brown 	}
6024103a8feaSLen Brown 
6025b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0);
6026b7d8c148SLen Brown 
6027b7d8c148SLen Brown 	flush_output_stderr();
6028103a8feaSLen Brown 
6029d91bb17cSLen Brown 	return status;
6030103a8feaSLen Brown }
6031103a8feaSLen Brown 
60323b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
60333b4d5c7fSAndy Shevchenko {
60343b4d5c7fSAndy Shevchenko 	int status;
60353b4d5c7fSAndy Shevchenko 
6036218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
60373b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
60383b4d5c7fSAndy Shevchenko 	if (status)
60393b4d5c7fSAndy Shevchenko 		return status;
60403b4d5c7fSAndy Shevchenko 
60413b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
60423b4d5c7fSAndy Shevchenko 	if (status)
60433b4d5c7fSAndy Shevchenko 		return status;
60443b4d5c7fSAndy Shevchenko 
6045b7d8c148SLen Brown 	flush_output_stdout();
60463b4d5c7fSAndy Shevchenko 
60473b4d5c7fSAndy Shevchenko 	return status;
60483b4d5c7fSAndy Shevchenko }
60493b4d5c7fSAndy Shevchenko 
60501b439f01SLen Brown void print_version()
60511b439f01SLen Brown {
60523c070b2aSLen Brown 	fprintf(outf, "turbostat version 21.05.04" " - Len Brown <lenb@kernel.org>\n");
6053d8af6f5fSLen Brown }
6054d8af6f5fSLen Brown 
6055495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
6056495c7654SLen Brown 		unsigned int width, enum counter_scope scope,
605741618e63SLen Brown 		enum counter_type type, enum counter_format format, int flags)
6058388e9c81SLen Brown {
6059388e9c81SLen Brown 	struct msr_counter *msrp;
6060388e9c81SLen Brown 
6061388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
6062388e9c81SLen Brown 	if (msrp == NULL) {
6063388e9c81SLen Brown 		perror("calloc");
6064388e9c81SLen Brown 		exit(1);
6065388e9c81SLen Brown 	}
6066388e9c81SLen Brown 
6067388e9c81SLen Brown 	msrp->msr_num = msr_num;
6068d8d005baSLen Brown 	strncpy(msrp->name, name, NAME_BYTES - 1);
6069495c7654SLen Brown 	if (path)
6070d8d005baSLen Brown 		strncpy(msrp->path, path, PATH_BYTES - 1);
6071388e9c81SLen Brown 	msrp->width = width;
6072388e9c81SLen Brown 	msrp->type = type;
6073388e9c81SLen Brown 	msrp->format = format;
607441618e63SLen Brown 	msrp->flags = flags;
6075388e9c81SLen Brown 
6076388e9c81SLen Brown 	switch (scope) {
6077388e9c81SLen Brown 
6078388e9c81SLen Brown 	case SCOPE_CPU:
6079388e9c81SLen Brown 		msrp->next = sys.tp;
6080388e9c81SLen Brown 		sys.tp = msrp;
6081678a3bd1SLen Brown 		sys.added_thread_counters++;
60820748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
60831b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS);
6084678a3bd1SLen Brown 			exit(-1);
6085678a3bd1SLen Brown 		}
6086388e9c81SLen Brown 		break;
6087388e9c81SLen Brown 
6088388e9c81SLen Brown 	case SCOPE_CORE:
6089388e9c81SLen Brown 		msrp->next = sys.cp;
6090388e9c81SLen Brown 		sys.cp = msrp;
6091678a3bd1SLen Brown 		sys.added_core_counters++;
6092678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
60931b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS);
6094678a3bd1SLen Brown 			exit(-1);
6095678a3bd1SLen Brown 		}
6096388e9c81SLen Brown 		break;
6097388e9c81SLen Brown 
6098388e9c81SLen Brown 	case SCOPE_PACKAGE:
6099388e9c81SLen Brown 		msrp->next = sys.pp;
6100388e9c81SLen Brown 		sys.pp = msrp;
6101678a3bd1SLen Brown 		sys.added_package_counters++;
6102678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
61031b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS);
6104678a3bd1SLen Brown 			exit(-1);
6105678a3bd1SLen Brown 		}
6106388e9c81SLen Brown 		break;
6107388e9c81SLen Brown 	}
6108388e9c81SLen Brown 
6109388e9c81SLen Brown 	return 0;
6110388e9c81SLen Brown }
6111388e9c81SLen Brown 
6112388e9c81SLen Brown void parse_add_command(char *add_command)
6113388e9c81SLen Brown {
6114388e9c81SLen Brown 	int msr_num = 0;
6115495c7654SLen Brown 	char *path = NULL;
61160f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
6117388e9c81SLen Brown 	int width = 64;
6118388e9c81SLen Brown 	int fail = 0;
6119388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
6120388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
6121388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
6122388e9c81SLen Brown 
6123388e9c81SLen Brown 	while (add_command) {
6124388e9c81SLen Brown 
6125388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6126388e9c81SLen Brown 			goto next;
6127388e9c81SLen Brown 
6128388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
6129388e9c81SLen Brown 			goto next;
6130388e9c81SLen Brown 
6131495c7654SLen Brown 		if (*add_command == '/') {
6132495c7654SLen Brown 			path = add_command;
6133495c7654SLen Brown 			goto next;
6134495c7654SLen Brown 		}
6135495c7654SLen Brown 
6136388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
6137388e9c81SLen Brown 			if ((width == 32) || (width == 64))
6138388e9c81SLen Brown 				goto next;
6139388e9c81SLen Brown 			width = 64;
6140388e9c81SLen Brown 		}
6141388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6142388e9c81SLen Brown 			scope = SCOPE_CPU;
6143388e9c81SLen Brown 			goto next;
6144388e9c81SLen Brown 		}
6145388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
6146388e9c81SLen Brown 			scope = SCOPE_CORE;
6147388e9c81SLen Brown 			goto next;
6148388e9c81SLen Brown 		}
6149388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
6150388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
6151388e9c81SLen Brown 			goto next;
6152388e9c81SLen Brown 		}
6153388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6154388e9c81SLen Brown 			type = COUNTER_CYCLES;
6155388e9c81SLen Brown 			goto next;
6156388e9c81SLen Brown 		}
6157388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6158388e9c81SLen Brown 			type = COUNTER_SECONDS;
6159388e9c81SLen Brown 			goto next;
6160388e9c81SLen Brown 		}
616141618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
616241618e63SLen Brown 			type = COUNTER_USEC;
616341618e63SLen Brown 			goto next;
616441618e63SLen Brown 		}
6165388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
6166388e9c81SLen Brown 			format = FORMAT_RAW;
6167388e9c81SLen Brown 			goto next;
6168388e9c81SLen Brown 		}
6169388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
6170388e9c81SLen Brown 			format = FORMAT_DELTA;
6171388e9c81SLen Brown 			goto next;
6172388e9c81SLen Brown 		}
6173388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
6174388e9c81SLen Brown 			format = FORMAT_PERCENT;
6175388e9c81SLen Brown 			goto next;
6176388e9c81SLen Brown 		}
6177388e9c81SLen Brown 
6178388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
6179388e9c81SLen Brown 			char *eos;
6180388e9c81SLen Brown 
6181388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
6182388e9c81SLen Brown 			if (eos)
6183388e9c81SLen Brown 				*eos = '\0';
6184388e9c81SLen Brown 			goto next;
6185388e9c81SLen Brown 		}
6186388e9c81SLen Brown 
6187388e9c81SLen Brown next:
6188388e9c81SLen Brown 		add_command = strchr(add_command, ',');
6189495c7654SLen Brown 		if (add_command) {
6190495c7654SLen Brown 			*add_command = '\0';
6191388e9c81SLen Brown 			add_command++;
6192495c7654SLen Brown 		}
6193388e9c81SLen Brown 
6194388e9c81SLen Brown 	}
6195495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
6196495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6197388e9c81SLen Brown 		fail++;
6198388e9c81SLen Brown 	}
6199388e9c81SLen Brown 
6200388e9c81SLen Brown 	/* generate default column header */
6201388e9c81SLen Brown 	if (*name_buffer == '\0') {
6202388e9c81SLen Brown 		if (width == 32)
62035f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6204388e9c81SLen Brown 		else
62055f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6206388e9c81SLen Brown 	}
6207388e9c81SLen Brown 
620841618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6209388e9c81SLen Brown 		fail++;
6210388e9c81SLen Brown 
6211388e9c81SLen Brown 	if (fail) {
6212388e9c81SLen Brown 		help();
6213388e9c81SLen Brown 		exit(1);
6214388e9c81SLen Brown 	}
6215388e9c81SLen Brown }
621641618e63SLen Brown 
62170fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name)
62180fc521bcSZephaniah E. Loss-Cutler-Hull {
62190fc521bcSZephaniah E. Loss-Cutler-Hull 	int i;
62200fc521bcSZephaniah E. Loss-Cutler-Hull 
62210fc521bcSZephaniah E. Loss-Cutler-Hull 	for (i = 0; i < deferred_add_index; ++i)
62220fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!strcmp(name, deferred_add_names[i]))
62230fc521bcSZephaniah E. Loss-Cutler-Hull 			return 1;
62240fc521bcSZephaniah E. Loss-Cutler-Hull 	return 0;
62250fc521bcSZephaniah E. Loss-Cutler-Hull }
62260fc521bcSZephaniah E. Loss-Cutler-Hull 
6227dd778a5eSLen Brown int is_deferred_skip(char *name)
6228dd778a5eSLen Brown {
6229dd778a5eSLen Brown 	int i;
6230dd778a5eSLen Brown 
6231dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
6232dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
6233dd778a5eSLen Brown 			return 1;
6234dd778a5eSLen Brown 	return 0;
6235dd778a5eSLen Brown }
6236dd778a5eSLen Brown 
623741618e63SLen Brown void probe_sysfs(void)
623841618e63SLen Brown {
623941618e63SLen Brown 	char path[64];
624041618e63SLen Brown 	char name_buf[16];
624141618e63SLen Brown 	FILE *input;
624241618e63SLen Brown 	int state;
624341618e63SLen Brown 	char *sp;
624441618e63SLen Brown 
62450748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
624641618e63SLen Brown 
62471b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
624841618e63SLen Brown 		input = fopen(path, "r");
624941618e63SLen Brown 		if (input == NULL)
625041618e63SLen Brown 			continue;
62518173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
62528173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
625341618e63SLen Brown 
625441618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
625541618e63SLen Brown 		sp = strchr(name_buf, '-');
625641618e63SLen Brown 		if (!sp)
625741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
625841618e63SLen Brown 		*sp = '%';
625941618e63SLen Brown 		*(sp + 1) = '\0';
626041618e63SLen Brown 
6261fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
6262fecb3bc8SDavid Arcari 
626341618e63SLen Brown 		fclose(input);
626441618e63SLen Brown 
626541618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
626641618e63SLen Brown 
62670fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
62680fc521bcSZephaniah E. Loss-Cutler-Hull 			continue;
62690fc521bcSZephaniah E. Loss-Cutler-Hull 
6270dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
6271dd778a5eSLen Brown 			continue;
6272dd778a5eSLen Brown 
62731b439f01SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU);
627441618e63SLen Brown 	}
627541618e63SLen Brown 
62760748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
627741618e63SLen Brown 
62781b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
627941618e63SLen Brown 		input = fopen(path, "r");
628041618e63SLen Brown 		if (input == NULL)
628141618e63SLen Brown 			continue;
62828173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
62838173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
628441618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
628541618e63SLen Brown 		sp = strchr(name_buf, '-');
628641618e63SLen Brown 		if (!sp)
628741618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
628841618e63SLen Brown 		*sp = '\0';
628941618e63SLen Brown 		fclose(input);
629041618e63SLen Brown 
6291fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
6292fecb3bc8SDavid Arcari 
629341618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
629441618e63SLen Brown 
62950fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
62960fc521bcSZephaniah E. Loss-Cutler-Hull 			continue;
62970fc521bcSZephaniah E. Loss-Cutler-Hull 
6298dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
6299dd778a5eSLen Brown 			continue;
6300dd778a5eSLen Brown 
63011b439f01SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU);
630241618e63SLen Brown 	}
630341618e63SLen Brown 
630441618e63SLen Brown }
630541618e63SLen Brown 
63061ef7d21aSLen Brown /*
63071ef7d21aSLen Brown  * parse cpuset with following syntax
63081ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
63091ef7d21aSLen Brown  */
63101ef7d21aSLen Brown void parse_cpu_command(char *optarg)
63111ef7d21aSLen Brown {
63121ef7d21aSLen Brown 	unsigned int start, end;
63131ef7d21aSLen Brown 	char *next;
63141ef7d21aSLen Brown 
63154e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
63164e4e1e7cSLen Brown 		if (cpu_subset)
63174e4e1e7cSLen Brown 			goto error;
63184e4e1e7cSLen Brown 		show_core_only++;
63194e4e1e7cSLen Brown 		return;
63204e4e1e7cSLen Brown 	}
63214e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
63224e4e1e7cSLen Brown 		if (cpu_subset)
63234e4e1e7cSLen Brown 			goto error;
63244e4e1e7cSLen Brown 		show_pkg_only++;
63254e4e1e7cSLen Brown 		return;
63264e4e1e7cSLen Brown 	}
63274e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
63284e4e1e7cSLen Brown 		goto error;
63294e4e1e7cSLen Brown 
63301ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
63311ef7d21aSLen Brown 	if (cpu_subset == NULL)
63321ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
63331ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
63341ef7d21aSLen Brown 
63351ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
63361ef7d21aSLen Brown 
63371ef7d21aSLen Brown 	next = optarg;
63381ef7d21aSLen Brown 
63391ef7d21aSLen Brown 	while (next && *next) {
63401ef7d21aSLen Brown 
63411ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
63421ef7d21aSLen Brown 			goto error;
63431ef7d21aSLen Brown 
63441ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
63451ef7d21aSLen Brown 
63461ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
63471ef7d21aSLen Brown 			goto error;
63481ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
63491ef7d21aSLen Brown 
63501ef7d21aSLen Brown 		if (*next == '\0')
63511ef7d21aSLen Brown 			break;
63521ef7d21aSLen Brown 
63531ef7d21aSLen Brown 		if (*next == ',') {
63541ef7d21aSLen Brown 			next += 1;
63551ef7d21aSLen Brown 			continue;
63561ef7d21aSLen Brown 		}
63571ef7d21aSLen Brown 
63581ef7d21aSLen Brown 		if (*next == '-') {
63591ef7d21aSLen Brown 			next += 1;	/* start range */
63601ef7d21aSLen Brown 		} else if (*next == '.') {
63611ef7d21aSLen Brown 			next += 1;
63621ef7d21aSLen Brown 			if (*next == '.')
63631ef7d21aSLen Brown 				next += 1;	/* start range */
63641ef7d21aSLen Brown 			else
63651ef7d21aSLen Brown 				goto error;
63661ef7d21aSLen Brown 		}
63671ef7d21aSLen Brown 
63681ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
63691ef7d21aSLen Brown 		if (end <= start)
63701ef7d21aSLen Brown 			goto error;
63711ef7d21aSLen Brown 
63721ef7d21aSLen Brown 		while (++start <= end) {
63731ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
63741ef7d21aSLen Brown 				goto error;
63751ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
63761ef7d21aSLen Brown 		}
63771ef7d21aSLen Brown 
63781ef7d21aSLen Brown 		if (*next == ',')
63791ef7d21aSLen Brown 			next += 1;
63801ef7d21aSLen Brown 		else if (*next != '\0')
63811ef7d21aSLen Brown 			goto error;
63821ef7d21aSLen Brown 	}
63831ef7d21aSLen Brown 
63841ef7d21aSLen Brown 	return;
63851ef7d21aSLen Brown 
63861ef7d21aSLen Brown error:
63874e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
63884e4e1e7cSLen Brown 	help();
63891ef7d21aSLen Brown 	exit(-1);
63901ef7d21aSLen Brown }
63911ef7d21aSLen Brown 
6392103a8feaSLen Brown void cmdline(int argc, char **argv)
6393103a8feaSLen Brown {
6394103a8feaSLen Brown 	int opt;
6395d8af6f5fSLen Brown 	int option_index = 0;
6396d8af6f5fSLen Brown 	static struct option long_options[] = {
6397388e9c81SLen Brown 		{ "add", required_argument, 0, 'a' },
63981ef7d21aSLen Brown 		{ "cpu", required_argument, 0, 'c' },
6399d8af6f5fSLen Brown 		{ "Dump", no_argument, 0, 'D' },
640096e47158SLen Brown 		{ "debug", no_argument, 0, 'd' },	/* internal, not documented */
64013f44a5c6SLen Brown 		{ "enable", required_argument, 0, 'e' },
6402d8af6f5fSLen Brown 		{ "interval", required_argument, 0, 'i' },
64032af4f9b8SLen Brown 		{ "IPC", no_argument, 0, 'I' },
6404023fe0acSChen Yu 		{ "num_iterations", required_argument, 0, 'n' },
6405c7e399f8SZephaniah E. Loss-Cutler-Hull 		{ "header_iterations", required_argument, 0, 'N' },
6406d8af6f5fSLen Brown 		{ "help", no_argument, 0, 'h' },
6407812db3f7SLen Brown 		{ "hide", required_argument, 0, 'H' },	// meh, -h taken by --help
6408d8af6f5fSLen Brown 		{ "Joules", no_argument, 0, 'J' },
6409c8ade361SLen Brown 		{ "list", no_argument, 0, 'l' },
6410b7d8c148SLen Brown 		{ "out", required_argument, 0, 'o' },
641196e47158SLen Brown 		{ "quiet", no_argument, 0, 'q' },
6412812db3f7SLen Brown 		{ "show", required_argument, 0, 's' },
6413d8af6f5fSLen Brown 		{ "Summary", no_argument, 0, 'S' },
6414d8af6f5fSLen Brown 		{ "TCC", required_argument, 0, 'T' },
6415d8af6f5fSLen Brown 		{ "version", no_argument, 0, 'v' },
6416d8af6f5fSLen Brown 		{ 0, 0, 0, 0 }
6417d8af6f5fSLen Brown 	};
6418103a8feaSLen Brown 
6419103a8feaSLen Brown 	progname = argv[0];
6420103a8feaSLen Brown 
64211b439f01SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) {
6422103a8feaSLen Brown 		switch (opt) {
6423388e9c81SLen Brown 		case 'a':
6424388e9c81SLen Brown 			parse_add_command(optarg);
6425388e9c81SLen Brown 			break;
64261ef7d21aSLen Brown 		case 'c':
64271ef7d21aSLen Brown 			parse_cpu_command(optarg);
64281ef7d21aSLen Brown 			break;
6429d8af6f5fSLen Brown 		case 'D':
6430d8af6f5fSLen Brown 			dump_only++;
64318e180f3cSLen Brown 			break;
64323f44a5c6SLen Brown 		case 'e':
64333f44a5c6SLen Brown 			/* --enable specified counter */
64344c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
64353f44a5c6SLen Brown 			break;
6436d8af6f5fSLen Brown 		case 'd':
6437d8af6f5fSLen Brown 			debug++;
64383f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
64392f32edf1SLen Brown 			break;
6440812db3f7SLen Brown 		case 'H':
64413f44a5c6SLen Brown 			/*
64423f44a5c6SLen Brown 			 * --hide: do not show those specified
64433f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
64443f44a5c6SLen Brown 			 */
64453f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6446812db3f7SLen Brown 			break;
6447d8af6f5fSLen Brown 		case 'h':
6448d8af6f5fSLen Brown 		default:
6449d8af6f5fSLen Brown 			help();
6450d8af6f5fSLen Brown 			exit(1);
6451d8af6f5fSLen Brown 		case 'i':
64522a0609c0SLen Brown 			{
64532a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
64542a0609c0SLen Brown 
64552a0609c0SLen Brown 				if (interval < 0.001) {
64561b439f01SLen Brown 					fprintf(outf, "interval %f seconds is too small\n", interval);
64572a0609c0SLen Brown 					exit(2);
64582a0609c0SLen Brown 				}
64592a0609c0SLen Brown 
646047936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
6461b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
646247936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
64632a0609c0SLen Brown 			}
6464889facbeSLen Brown 			break;
64655c56be9aSDirk Brandewie 		case 'J':
64665c56be9aSDirk Brandewie 			rapl_joules++;
64675c56be9aSDirk Brandewie 			break;
6468c8ade361SLen Brown 		case 'l':
64693f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6470c8ade361SLen Brown 			list_header_only++;
6471c8ade361SLen Brown 			quiet++;
6472c8ade361SLen Brown 			break;
6473b7d8c148SLen Brown 		case 'o':
6474b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
6475b7d8c148SLen Brown 			break;
647696e47158SLen Brown 		case 'q':
647796e47158SLen Brown 			quiet = 1;
647896e47158SLen Brown 			break;
6479023fe0acSChen Yu 		case 'n':
6480023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
6481023fe0acSChen Yu 
6482023fe0acSChen Yu 			if (num_iterations <= 0) {
64831b439f01SLen Brown 				fprintf(outf, "iterations %d should be positive number\n", num_iterations);
6484023fe0acSChen Yu 				exit(2);
6485023fe0acSChen Yu 			}
6486023fe0acSChen Yu 			break;
6487c7e399f8SZephaniah E. Loss-Cutler-Hull 		case 'N':
6488c7e399f8SZephaniah E. Loss-Cutler-Hull 			header_iterations = strtod(optarg, NULL);
6489c7e399f8SZephaniah E. Loss-Cutler-Hull 
6490c7e399f8SZephaniah E. Loss-Cutler-Hull 			if (header_iterations <= 0) {
6491*164d7a96SLen Brown 				fprintf(outf, "iterations %d should be positive number\n", header_iterations);
6492c7e399f8SZephaniah E. Loss-Cutler-Hull 				exit(2);
6493c7e399f8SZephaniah E. Loss-Cutler-Hull 			}
6494c7e399f8SZephaniah E. Loss-Cutler-Hull 			break;
6495812db3f7SLen Brown 		case 's':
64963f44a5c6SLen Brown 			/*
64973f44a5c6SLen Brown 			 * --show: show only those specified
64983f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
64993f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
65003f44a5c6SLen Brown 			 */
65013f44a5c6SLen Brown 			if (shown == 0)
65023f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
65033f44a5c6SLen Brown 			else
65043f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
65053f44a5c6SLen Brown 			shown = 1;
6506812db3f7SLen Brown 			break;
6507d8af6f5fSLen Brown 		case 'S':
6508d8af6f5fSLen Brown 			summary_only++;
6509d8af6f5fSLen Brown 			break;
6510d8af6f5fSLen Brown 		case 'T':
651155279aefSZhang Rui 			tj_max_override = atoi(optarg);
6512d8af6f5fSLen Brown 			break;
6513d8af6f5fSLen Brown 		case 'v':
6514d8af6f5fSLen Brown 			print_version();
6515d8af6f5fSLen Brown 			exit(0);
6516d8af6f5fSLen Brown 			break;
6517103a8feaSLen Brown 		}
6518103a8feaSLen Brown 	}
6519103a8feaSLen Brown }
6520103a8feaSLen Brown 
6521103a8feaSLen Brown int main(int argc, char **argv)
6522103a8feaSLen Brown {
6523b7d8c148SLen Brown 	outf = stderr;
6524103a8feaSLen Brown 	cmdline(argc, argv);
6525103a8feaSLen Brown 
652696e47158SLen Brown 	if (!quiet)
6527d8af6f5fSLen Brown 		print_version();
6528103a8feaSLen Brown 
652941618e63SLen Brown 	probe_sysfs();
653041618e63SLen Brown 
6531103a8feaSLen Brown 	turbostat_init();
6532103a8feaSLen Brown 
65336799ba84SDan Merillat 	msr_sum_record();
65346799ba84SDan Merillat 
65353b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
65363b4d5c7fSAndy Shevchenko 	if (dump_only)
65373b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
65383b4d5c7fSAndy Shevchenko 
6539c8ade361SLen Brown 	/* list header and exit */
6540c8ade361SLen Brown 	if (list_header_only) {
6541c8ade361SLen Brown 		print_header(",");
6542c8ade361SLen Brown 		flush_output_stdout();
6543c8ade361SLen Brown 		return 0;
6544c8ade361SLen Brown 	}
6545c8ade361SLen Brown 
6546103a8feaSLen Brown 	/*
6547103a8feaSLen Brown 	 * if any params left, it must be a command to fork
6548103a8feaSLen Brown 	 */
6549103a8feaSLen Brown 	if (argc - optind)
6550103a8feaSLen Brown 		return fork_it(argv + optind);
6551103a8feaSLen Brown 	else
6552103a8feaSLen Brown 		turbostat_loop();
6553103a8feaSLen Brown 
6554103a8feaSLen Brown 	return 0;
6555103a8feaSLen Brown }
6556