xref: /openbmc/linux/tools/power/x86/turbostat/turbostat.c (revision 0e4d42af81471ab801e059e1feac8e27547230cf)
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  *
658990892SLen Brown  * Copyright (c) 2022 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 
409878bf7aSLen Brown #define UNUSED(x) (void)(x)
419878bf7aSLen Brown 
42164d7a96SLen Brown /*
43164d7a96SLen Brown  * This list matches the column headers, except
44164d7a96SLen Brown  * 1. built-in only, the sysfs counters are not here -- we learn of those at run-time
45164d7a96SLen Brown  * 2. Core and CPU are moved to the end, we can't have strings that contain them
46164d7a96SLen Brown  *    matching on them for --show and --hide.
47164d7a96SLen Brown  */
48164d7a96SLen Brown 
49164d7a96SLen Brown /*
50164d7a96SLen Brown  * buffer size used by sscanf() for added column names
51164d7a96SLen Brown  * Usually truncated to 7 characters, but also handles 18 columns for raw 64-bit counters
52164d7a96SLen Brown  */
53164d7a96SLen Brown #define	NAME_BYTES 20
54164d7a96SLen Brown #define PATH_BYTES 128
55164d7a96SLen Brown 
56164d7a96SLen Brown enum counter_scope { SCOPE_CPU, SCOPE_CORE, SCOPE_PACKAGE };
57164d7a96SLen Brown enum counter_type { COUNTER_ITEMS, COUNTER_CYCLES, COUNTER_SECONDS, COUNTER_USEC };
58164d7a96SLen Brown enum counter_format { FORMAT_RAW, FORMAT_DELTA, FORMAT_PERCENT };
59164d7a96SLen Brown 
60164d7a96SLen Brown struct msr_counter {
61164d7a96SLen Brown 	unsigned int msr_num;
62164d7a96SLen Brown 	char name[NAME_BYTES];
63164d7a96SLen Brown 	char path[PATH_BYTES];
64164d7a96SLen Brown 	unsigned int width;
65164d7a96SLen Brown 	enum counter_type type;
66164d7a96SLen Brown 	enum counter_format format;
67164d7a96SLen Brown 	struct msr_counter *next;
68164d7a96SLen Brown 	unsigned int flags;
69164d7a96SLen Brown #define	FLAGS_HIDE	(1 << 0)
70164d7a96SLen Brown #define	FLAGS_SHOW	(1 << 1)
71164d7a96SLen Brown #define	SYSFS_PERCPU	(1 << 1)
72164d7a96SLen Brown };
73164d7a96SLen Brown 
74164d7a96SLen Brown struct msr_counter bic[] = {
759878bf7aSLen Brown 	{ 0x0, "usec", "", 0, 0, 0, NULL, 0 },
769878bf7aSLen Brown 	{ 0x0, "Time_Of_Day_Seconds", "", 0, 0, 0, NULL, 0 },
779878bf7aSLen Brown 	{ 0x0, "Package", "", 0, 0, 0, NULL, 0 },
789878bf7aSLen Brown 	{ 0x0, "Node", "", 0, 0, 0, NULL, 0 },
799878bf7aSLen Brown 	{ 0x0, "Avg_MHz", "", 0, 0, 0, NULL, 0 },
809878bf7aSLen Brown 	{ 0x0, "Busy%", "", 0, 0, 0, NULL, 0 },
819878bf7aSLen Brown 	{ 0x0, "Bzy_MHz", "", 0, 0, 0, NULL, 0 },
829878bf7aSLen Brown 	{ 0x0, "TSC_MHz", "", 0, 0, 0, NULL, 0 },
839878bf7aSLen Brown 	{ 0x0, "IRQ", "", 0, 0, 0, NULL, 0 },
849878bf7aSLen Brown 	{ 0x0, "SMI", "", 32, 0, FORMAT_DELTA, NULL, 0 },
859878bf7aSLen Brown 	{ 0x0, "sysfs", "", 0, 0, 0, NULL, 0 },
869878bf7aSLen Brown 	{ 0x0, "CPU%c1", "", 0, 0, 0, NULL, 0 },
879878bf7aSLen Brown 	{ 0x0, "CPU%c3", "", 0, 0, 0, NULL, 0 },
889878bf7aSLen Brown 	{ 0x0, "CPU%c6", "", 0, 0, 0, NULL, 0 },
899878bf7aSLen Brown 	{ 0x0, "CPU%c7", "", 0, 0, 0, NULL, 0 },
909878bf7aSLen Brown 	{ 0x0, "ThreadC", "", 0, 0, 0, NULL, 0 },
919878bf7aSLen Brown 	{ 0x0, "CoreTmp", "", 0, 0, 0, NULL, 0 },
929878bf7aSLen Brown 	{ 0x0, "CoreCnt", "", 0, 0, 0, NULL, 0 },
939878bf7aSLen Brown 	{ 0x0, "PkgTmp", "", 0, 0, 0, NULL, 0 },
949878bf7aSLen Brown 	{ 0x0, "GFX%rc6", "", 0, 0, 0, NULL, 0 },
959878bf7aSLen Brown 	{ 0x0, "GFXMHz", "", 0, 0, 0, NULL, 0 },
969878bf7aSLen Brown 	{ 0x0, "Pkg%pc2", "", 0, 0, 0, NULL, 0 },
979878bf7aSLen Brown 	{ 0x0, "Pkg%pc3", "", 0, 0, 0, NULL, 0 },
989878bf7aSLen Brown 	{ 0x0, "Pkg%pc6", "", 0, 0, 0, NULL, 0 },
999878bf7aSLen Brown 	{ 0x0, "Pkg%pc7", "", 0, 0, 0, NULL, 0 },
1009878bf7aSLen Brown 	{ 0x0, "Pkg%pc8", "", 0, 0, 0, NULL, 0 },
1019878bf7aSLen Brown 	{ 0x0, "Pkg%pc9", "", 0, 0, 0, NULL, 0 },
1029878bf7aSLen Brown 	{ 0x0, "Pk%pc10", "", 0, 0, 0, NULL, 0 },
1039878bf7aSLen Brown 	{ 0x0, "CPU%LPI", "", 0, 0, 0, NULL, 0 },
1049878bf7aSLen Brown 	{ 0x0, "SYS%LPI", "", 0, 0, 0, NULL, 0 },
1059878bf7aSLen Brown 	{ 0x0, "PkgWatt", "", 0, 0, 0, NULL, 0 },
1069878bf7aSLen Brown 	{ 0x0, "CorWatt", "", 0, 0, 0, NULL, 0 },
1079878bf7aSLen Brown 	{ 0x0, "GFXWatt", "", 0, 0, 0, NULL, 0 },
1089878bf7aSLen Brown 	{ 0x0, "PkgCnt", "", 0, 0, 0, NULL, 0 },
1099878bf7aSLen Brown 	{ 0x0, "RAMWatt", "", 0, 0, 0, NULL, 0 },
1109878bf7aSLen Brown 	{ 0x0, "PKG_%", "", 0, 0, 0, NULL, 0 },
1119878bf7aSLen Brown 	{ 0x0, "RAM_%", "", 0, 0, 0, NULL, 0 },
1129878bf7aSLen Brown 	{ 0x0, "Pkg_J", "", 0, 0, 0, NULL, 0 },
1139878bf7aSLen Brown 	{ 0x0, "Cor_J", "", 0, 0, 0, NULL, 0 },
1149878bf7aSLen Brown 	{ 0x0, "GFX_J", "", 0, 0, 0, NULL, 0 },
1159878bf7aSLen Brown 	{ 0x0, "RAM_J", "", 0, 0, 0, NULL, 0 },
1169878bf7aSLen Brown 	{ 0x0, "Mod%c6", "", 0, 0, 0, NULL, 0 },
1179878bf7aSLen Brown 	{ 0x0, "Totl%C0", "", 0, 0, 0, NULL, 0 },
1189878bf7aSLen Brown 	{ 0x0, "Any%C0", "", 0, 0, 0, NULL, 0 },
1199878bf7aSLen Brown 	{ 0x0, "GFX%C0", "", 0, 0, 0, NULL, 0 },
1209878bf7aSLen Brown 	{ 0x0, "CPUGFX%", "", 0, 0, 0, NULL, 0 },
1219878bf7aSLen Brown 	{ 0x0, "Core", "", 0, 0, 0, NULL, 0 },
1229878bf7aSLen Brown 	{ 0x0, "CPU", "", 0, 0, 0, NULL, 0 },
1239878bf7aSLen Brown 	{ 0x0, "APIC", "", 0, 0, 0, NULL, 0 },
1249878bf7aSLen Brown 	{ 0x0, "X2APIC", "", 0, 0, 0, NULL, 0 },
1259878bf7aSLen Brown 	{ 0x0, "Die", "", 0, 0, 0, NULL, 0 },
1269878bf7aSLen Brown 	{ 0x0, "GFXAMHz", "", 0, 0, 0, NULL, 0 },
1279878bf7aSLen Brown 	{ 0x0, "IPC", "", 0, 0, 0, NULL, 0 },
1289878bf7aSLen Brown 	{ 0x0, "CoreThr", "", 0, 0, 0, NULL, 0 },
129a5c6d65dSLen Brown 	{ 0x0, "UncMHz", "", 0, 0, 0, NULL, 0 },
130164d7a96SLen Brown };
131164d7a96SLen Brown 
132164d7a96SLen Brown #define MAX_BIC (sizeof(bic) / sizeof(struct msr_counter))
133164d7a96SLen Brown #define	BIC_USEC	(1ULL << 0)
134164d7a96SLen Brown #define	BIC_TOD		(1ULL << 1)
135164d7a96SLen Brown #define	BIC_Package	(1ULL << 2)
136164d7a96SLen Brown #define	BIC_Node	(1ULL << 3)
137164d7a96SLen Brown #define	BIC_Avg_MHz	(1ULL << 4)
138164d7a96SLen Brown #define	BIC_Busy	(1ULL << 5)
139164d7a96SLen Brown #define	BIC_Bzy_MHz	(1ULL << 6)
140164d7a96SLen Brown #define	BIC_TSC_MHz	(1ULL << 7)
141164d7a96SLen Brown #define	BIC_IRQ		(1ULL << 8)
142164d7a96SLen Brown #define	BIC_SMI		(1ULL << 9)
143164d7a96SLen Brown #define	BIC_sysfs	(1ULL << 10)
144164d7a96SLen Brown #define	BIC_CPU_c1	(1ULL << 11)
145164d7a96SLen Brown #define	BIC_CPU_c3	(1ULL << 12)
146164d7a96SLen Brown #define	BIC_CPU_c6	(1ULL << 13)
147164d7a96SLen Brown #define	BIC_CPU_c7	(1ULL << 14)
148164d7a96SLen Brown #define	BIC_ThreadC	(1ULL << 15)
149164d7a96SLen Brown #define	BIC_CoreTmp	(1ULL << 16)
150164d7a96SLen Brown #define	BIC_CoreCnt	(1ULL << 17)
151164d7a96SLen Brown #define	BIC_PkgTmp	(1ULL << 18)
152164d7a96SLen Brown #define	BIC_GFX_rc6	(1ULL << 19)
153164d7a96SLen Brown #define	BIC_GFXMHz	(1ULL << 20)
154164d7a96SLen Brown #define	BIC_Pkgpc2	(1ULL << 21)
155164d7a96SLen Brown #define	BIC_Pkgpc3	(1ULL << 22)
156164d7a96SLen Brown #define	BIC_Pkgpc6	(1ULL << 23)
157164d7a96SLen Brown #define	BIC_Pkgpc7	(1ULL << 24)
158164d7a96SLen Brown #define	BIC_Pkgpc8	(1ULL << 25)
159164d7a96SLen Brown #define	BIC_Pkgpc9	(1ULL << 26)
160164d7a96SLen Brown #define	BIC_Pkgpc10	(1ULL << 27)
161164d7a96SLen Brown #define BIC_CPU_LPI	(1ULL << 28)
162164d7a96SLen Brown #define BIC_SYS_LPI	(1ULL << 29)
163164d7a96SLen Brown #define	BIC_PkgWatt	(1ULL << 30)
164164d7a96SLen Brown #define	BIC_CorWatt	(1ULL << 31)
165164d7a96SLen Brown #define	BIC_GFXWatt	(1ULL << 32)
166164d7a96SLen Brown #define	BIC_PkgCnt	(1ULL << 33)
167164d7a96SLen Brown #define	BIC_RAMWatt	(1ULL << 34)
168164d7a96SLen Brown #define	BIC_PKG__	(1ULL << 35)
169164d7a96SLen Brown #define	BIC_RAM__	(1ULL << 36)
170164d7a96SLen Brown #define	BIC_Pkg_J	(1ULL << 37)
171164d7a96SLen Brown #define	BIC_Cor_J	(1ULL << 38)
172164d7a96SLen Brown #define	BIC_GFX_J	(1ULL << 39)
173164d7a96SLen Brown #define	BIC_RAM_J	(1ULL << 40)
174164d7a96SLen Brown #define	BIC_Mod_c6	(1ULL << 41)
175164d7a96SLen Brown #define	BIC_Totl_c0	(1ULL << 42)
176164d7a96SLen Brown #define	BIC_Any_c0	(1ULL << 43)
177164d7a96SLen Brown #define	BIC_GFX_c0	(1ULL << 44)
178164d7a96SLen Brown #define	BIC_CPUGFX	(1ULL << 45)
179164d7a96SLen Brown #define	BIC_Core	(1ULL << 46)
180164d7a96SLen Brown #define	BIC_CPU		(1ULL << 47)
181164d7a96SLen Brown #define	BIC_APIC	(1ULL << 48)
182164d7a96SLen Brown #define	BIC_X2APIC	(1ULL << 49)
183164d7a96SLen Brown #define	BIC_Die		(1ULL << 50)
184164d7a96SLen Brown #define	BIC_GFXACTMHz	(1ULL << 51)
185164d7a96SLen Brown #define	BIC_IPC		(1ULL << 52)
186164d7a96SLen Brown #define	BIC_CORE_THROT_CNT	(1ULL << 53)
187a5c6d65dSLen Brown #define	BIC_UNCORE_MHZ		(1ULL << 54)
188164d7a96SLen Brown 
189164d7a96SLen Brown #define BIC_TOPOLOGY (BIC_Package | BIC_Node | BIC_CoreCnt | BIC_PkgCnt | BIC_Core | BIC_CPU | BIC_Die )
190164d7a96SLen Brown #define BIC_THERMAL_PWR ( BIC_CoreTmp | BIC_PkgTmp | BIC_PkgWatt | BIC_CorWatt | BIC_GFXWatt | BIC_RAMWatt | BIC_PKG__ | BIC_RAM__)
191a5c6d65dSLen Brown #define BIC_FREQUENCY ( BIC_Avg_MHz | BIC_Busy | BIC_Bzy_MHz | BIC_TSC_MHz | BIC_GFXMHz | BIC_GFXACTMHz | BIC_UNCORE_MHZ)
192164d7a96SLen 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)
193164d7a96SLen Brown #define BIC_OTHER ( BIC_IRQ | BIC_SMI | BIC_ThreadC | BIC_CoreTmp | BIC_IPC)
194164d7a96SLen Brown 
195164d7a96SLen Brown #define BIC_DISABLED_BY_DEFAULT	(BIC_USEC | BIC_TOD | BIC_APIC | BIC_X2APIC)
196164d7a96SLen Brown 
197164d7a96SLen Brown unsigned long long bic_enabled = (0xFFFFFFFFFFFFFFFFULL & ~BIC_DISABLED_BY_DEFAULT);
198164d7a96SLen Brown unsigned long long bic_present = BIC_USEC | BIC_TOD | BIC_sysfs | BIC_APIC | BIC_X2APIC;
199164d7a96SLen Brown 
200164d7a96SLen Brown #define DO_BIC(COUNTER_NAME) (bic_enabled & bic_present & COUNTER_NAME)
201164d7a96SLen Brown #define DO_BIC_READ(COUNTER_NAME) (bic_present & COUNTER_NAME)
202164d7a96SLen Brown #define ENABLE_BIC(COUNTER_NAME) (bic_enabled |= COUNTER_NAME)
203164d7a96SLen Brown #define BIC_PRESENT(COUNTER_BIT) (bic_present |= COUNTER_BIT)
204164d7a96SLen Brown #define BIC_NOT_PRESENT(COUNTER_BIT) (bic_present &= ~COUNTER_BIT)
205164d7a96SLen Brown #define BIC_IS_ENABLED(COUNTER_BIT) (bic_enabled & COUNTER_BIT)
206164d7a96SLen Brown 
207103a8feaSLen Brown char *proc_stat = "/proc/stat";
208b7d8c148SLen Brown FILE *outf;
20936229897SLen Brown int *fd_percpu;
2102af4f9b8SLen Brown int *fd_instr_count_percpu;
211b9ad8ee0SLen Brown struct timeval interval_tv = { 5, 0 };
21247936f94SArtem Bityutskiy struct timespec interval_ts = { 5, 0 };
213e9d3092fSZhang Rui 
214e9d3092fSZhang Rui /* Save original CPU model */
215e9d3092fSZhang Rui unsigned int model_orig;
216e9d3092fSZhang Rui 
217023fe0acSChen Yu unsigned int num_iterations;
218c7e399f8SZephaniah E. Loss-Cutler-Hull unsigned int header_iterations;
219d8af6f5fSLen Brown unsigned int debug;
22096e47158SLen Brown unsigned int quiet;
2213f44a5c6SLen Brown unsigned int shown;
2220de6c0dfSLen Brown unsigned int sums_need_wide_columns;
223d8af6f5fSLen Brown unsigned int rapl_joules;
224d8af6f5fSLen Brown unsigned int summary_only;
225c8ade361SLen Brown unsigned int list_header_only;
226d8af6f5fSLen Brown unsigned int dump_only;
227103a8feaSLen Brown unsigned int do_snb_cstates;
228fb5d4327SDasaratharaman Chandramouli unsigned int do_knl_cstates;
229144b44b1SLen Brown unsigned int do_slm_cstates;
230144b44b1SLen Brown unsigned int use_c1_residency_msr;
231103a8feaSLen Brown unsigned int has_aperf;
232889facbeSLen Brown unsigned int has_epb;
233774627c5SLen Brown unsigned int is_hybrid;
2345a63426eSLen Brown unsigned int do_irtl_snb;
2355a63426eSLen Brown unsigned int do_irtl_hsw;
236fc04cc67SLen Brown unsigned int units = 1000000;	/* MHz etc */
237103a8feaSLen Brown unsigned int genuine_intel;
23834041551SLen Brown unsigned int authentic_amd;
239c1c10cc7SPu Wen unsigned int hygon_genuine;
24034041551SLen Brown unsigned int max_level, max_extended_level;
241103a8feaSLen Brown unsigned int has_invariant_tsc;
242d7899447SLen Brown unsigned int do_nhm_platform_info;
243cf4cbe53SLen Brown unsigned int no_MSR_MISC_PWR_MGMT;
244b2b34dfeSHubert Chrzaniuk unsigned int aperf_mperf_multiplier = 1;
245103a8feaSLen Brown double bclk;
246a2b7b749SLen Brown double base_hz;
24721ed5574SLen Brown unsigned int has_base_hz;
248a2b7b749SLen Brown double tsc_tweak = 1.0;
249c98d5d94SLen Brown unsigned int show_pkg_only;
250c98d5d94SLen Brown unsigned int show_core_only;
251c98d5d94SLen Brown char *output_buffer, *outp;
252889facbeSLen Brown unsigned int do_rapl;
253889facbeSLen Brown unsigned int do_dts;
254889facbeSLen Brown unsigned int do_ptm;
2552af4f9b8SLen Brown unsigned int do_ipc;
256fdf676e5SLen Brown unsigned long long gfx_cur_rc6_ms;
257be0e54c4SLen Brown unsigned long long cpuidle_cur_cpu_lpi_us;
258be0e54c4SLen Brown unsigned long long cpuidle_cur_sys_lpi_us;
25927d47356SLen Brown unsigned int gfx_cur_mhz;
260b4b91569SRafael Antognolli unsigned int gfx_act_mhz;
26155279aefSZhang Rui unsigned int tj_max;
26255279aefSZhang Rui unsigned int tj_max_override;
2630b9a0b9bSZhang Rui int tcc_offset_bits;
26440ee8e3bSAndrey Semin double rapl_power_units, rapl_time_units;
26540ee8e3bSAndrey Semin double rapl_dram_energy_units, rapl_energy_units;
266889facbeSLen Brown double rapl_joule_counter_range;
2673a9a941dSLen Brown unsigned int do_core_perf_limit_reasons;
268ac980e13SArtem Bityutskiy unsigned int has_automatic_cstate_conversion;
269aeb01e6dSChen Yu unsigned int dis_cstate_prewake;
2703a9a941dSLen Brown unsigned int do_gfx_perf_limit_reasons;
2713a9a941dSLen Brown unsigned int do_ring_perf_limit_reasons;
2728a5bdf41SLen Brown unsigned int crystal_hz;
2738a5bdf41SLen Brown unsigned long long tsc_hz;
2747ce7d5deSPrarit Bhargava int base_cpu;
27521ed5574SLen Brown double discover_bclk(unsigned int family, unsigned int model);
2767f5c258eSLen Brown unsigned int has_hwp;		/* IA32_PM_ENABLE, IA32_HWP_CAPABILITIES */
2777f5c258eSLen Brown 			/* IA32_HWP_REQUEST, IA32_HWP_STATUS */
2787f5c258eSLen Brown unsigned int has_hwp_notify;	/* IA32_HWP_INTERRUPT */
2797f5c258eSLen Brown unsigned int has_hwp_activity_window;	/* IA32_HWP_REQUEST[bits 41:32] */
2807f5c258eSLen Brown unsigned int has_hwp_epp;	/* IA32_HWP_REQUEST[bits 31:24] */
2817f5c258eSLen Brown unsigned int has_hwp_pkg;	/* IA32_HWP_REQUEST_PKG */
28233148d67SLen Brown unsigned int has_misc_feature_control;
2834c2122d4SLen Brown unsigned int first_counter_read = 1;
284c026c236SArtem Bityutskiy int ignore_stdin;
285889facbeSLen Brown 
286889facbeSLen Brown #define RAPL_PKG		(1 << 0)
287e6f9bb3cSLen Brown 					/* 0x610 MSR_PKG_POWER_LIMIT */
288e6f9bb3cSLen Brown 					/* 0x611 MSR_PKG_ENERGY_STATUS */
289e6f9bb3cSLen Brown #define RAPL_PKG_PERF_STATUS	(1 << 1)
290e6f9bb3cSLen Brown 					/* 0x613 MSR_PKG_PERF_STATUS */
291e6f9bb3cSLen Brown #define RAPL_PKG_POWER_INFO	(1 << 2)
292e6f9bb3cSLen Brown 					/* 0x614 MSR_PKG_POWER_INFO */
293e6f9bb3cSLen Brown 
294889facbeSLen Brown #define RAPL_DRAM		(1 << 3)
295e6f9bb3cSLen Brown 					/* 0x618 MSR_DRAM_POWER_LIMIT */
296e6f9bb3cSLen Brown 					/* 0x619 MSR_DRAM_ENERGY_STATUS */
297e6f9bb3cSLen Brown #define RAPL_DRAM_PERF_STATUS	(1 << 4)
298e6f9bb3cSLen Brown 					/* 0x61b MSR_DRAM_PERF_STATUS */
2990b2bb692SLen Brown #define RAPL_DRAM_POWER_INFO	(1 << 5)
3000b2bb692SLen Brown 					/* 0x61c MSR_DRAM_POWER_INFO */
301e6f9bb3cSLen Brown 
3029148494cSJacob Pan #define RAPL_CORES_POWER_LIMIT	(1 << 6)
303e6f9bb3cSLen Brown 					/* 0x638 MSR_PP0_POWER_LIMIT */
3040b2bb692SLen Brown #define RAPL_CORE_POLICY	(1 << 7)
305e6f9bb3cSLen Brown 					/* 0x63a MSR_PP0_POLICY */
306e6f9bb3cSLen Brown 
3070b2bb692SLen Brown #define RAPL_GFX		(1 << 8)
308e6f9bb3cSLen Brown 					/* 0x640 MSR_PP1_POWER_LIMIT */
309e6f9bb3cSLen Brown 					/* 0x641 MSR_PP1_ENERGY_STATUS */
310e6f9bb3cSLen Brown 					/* 0x642 MSR_PP1_POLICY */
3119148494cSJacob Pan 
3129148494cSJacob Pan #define RAPL_CORES_ENERGY_STATUS	(1 << 9)
3139148494cSJacob Pan 					/* 0x639 MSR_PP0_ENERGY_STATUS */
3149392bd98SCalvin Walton #define RAPL_PER_CORE_ENERGY	(1 << 10)
3159392bd98SCalvin Walton 					/* Indicates cores energy collection is per-core,
3169392bd98SCalvin Walton 					 * not per-package. */
3179392bd98SCalvin Walton #define RAPL_AMD_F17H		(1 << 11)
3189392bd98SCalvin Walton 					/* 0xc0010299 MSR_RAPL_PWR_UNIT */
3199392bd98SCalvin Walton 					/* 0xc001029a MSR_CORE_ENERGY_STAT */
3209392bd98SCalvin Walton 					/* 0xc001029b MSR_PKG_ENERGY_STAT */
3219148494cSJacob Pan #define RAPL_CORES (RAPL_CORES_ENERGY_STATUS | RAPL_CORES_POWER_LIMIT)
322889facbeSLen Brown #define	TJMAX_DEFAULT	100
323889facbeSLen Brown 
3249392bd98SCalvin Walton /* MSRs that are not yet in the kernel-provided header. */
3259392bd98SCalvin Walton #define MSR_RAPL_PWR_UNIT	0xc0010299
3269392bd98SCalvin Walton #define MSR_CORE_ENERGY_STAT	0xc001029a
3279392bd98SCalvin Walton #define MSR_PKG_ENERGY_STAT	0xc001029b
3289392bd98SCalvin Walton 
329889facbeSLen Brown #define MAX(a, b) ((a) > (b) ? (a) : (b))
330103a8feaSLen Brown 
331103a8feaSLen Brown int backwards_count;
332103a8feaSLen Brown char *progname;
333103a8feaSLen Brown 
3341ef7d21aSLen Brown #define CPU_SUBSET_MAXCPUS	1024	/* need to use before probe... */
3351ef7d21aSLen Brown cpu_set_t *cpu_present_set, *cpu_affinity_set, *cpu_subset;
3361ef7d21aSLen Brown size_t cpu_present_setsize, cpu_affinity_setsize, cpu_subset_size;
3370748eaf0SLen Brown #define MAX_ADDED_COUNTERS 8
3380748eaf0SLen Brown #define MAX_ADDED_THREAD_COUNTERS 24
3390e2d8f05SLen Brown #define BITMASK_SIZE 32
340c98d5d94SLen Brown 
341c98d5d94SLen Brown struct thread_data {
342f4fdf2b4SLen Brown 	struct timeval tv_begin;
343f4fdf2b4SLen Brown 	struct timeval tv_end;
344d4794f25SYazen Ghannam 	struct timeval tv_delta;
345c98d5d94SLen Brown 	unsigned long long tsc;
346c98d5d94SLen Brown 	unsigned long long aperf;
347c98d5d94SLen Brown 	unsigned long long mperf;
348144b44b1SLen Brown 	unsigned long long c1;
3492af4f9b8SLen Brown 	unsigned long long instr_count;
3500de6c0dfSLen Brown 	unsigned long long irq_count;
3511ed51011SLen Brown 	unsigned int smi_count;
352c98d5d94SLen Brown 	unsigned int cpu_id;
3534c2122d4SLen Brown 	unsigned int apic_id;
3544c2122d4SLen Brown 	unsigned int x2apic_id;
355c98d5d94SLen Brown 	unsigned int flags;
3567ab5ff49SZhang Rui 	bool is_atom;
357c98d5d94SLen Brown #define CPU_IS_FIRST_THREAD_IN_CORE	0x2
358c98d5d94SLen Brown #define CPU_IS_FIRST_CORE_IN_PACKAGE	0x4
3590748eaf0SLen Brown 	unsigned long long counter[MAX_ADDED_THREAD_COUNTERS];
360c98d5d94SLen Brown } *thread_even, *thread_odd;
361c98d5d94SLen Brown 
362c98d5d94SLen Brown struct core_data {
363c98d5d94SLen Brown 	unsigned long long c3;
364c98d5d94SLen Brown 	unsigned long long c6;
365c98d5d94SLen Brown 	unsigned long long c7;
3660539ba11SLen Brown 	unsigned long long mc6_us;	/* duplicate as per-core for now, even though per module */
367889facbeSLen Brown 	unsigned int core_temp_c;
3689392bd98SCalvin Walton 	unsigned int core_energy;	/* MSR_CORE_ENERGY_STAT */
369c98d5d94SLen Brown 	unsigned int core_id;
370eae97e05SChen Yu 	unsigned long long core_throt_cnt;
371678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
372c98d5d94SLen Brown } *core_even, *core_odd;
373c98d5d94SLen Brown 
374c98d5d94SLen Brown struct pkg_data {
375c98d5d94SLen Brown 	unsigned long long pc2;
376c98d5d94SLen Brown 	unsigned long long pc3;
377c98d5d94SLen Brown 	unsigned long long pc6;
378c98d5d94SLen Brown 	unsigned long long pc7;
379ca58710fSKristen Carlson Accardi 	unsigned long long pc8;
380ca58710fSKristen Carlson Accardi 	unsigned long long pc9;
381ca58710fSKristen Carlson Accardi 	unsigned long long pc10;
382be0e54c4SLen Brown 	unsigned long long cpu_lpi;
383be0e54c4SLen Brown 	unsigned long long sys_lpi;
3840b2bb692SLen Brown 	unsigned long long pkg_wtd_core_c0;
3850b2bb692SLen Brown 	unsigned long long pkg_any_core_c0;
3860b2bb692SLen Brown 	unsigned long long pkg_any_gfxe_c0;
3870b2bb692SLen Brown 	unsigned long long pkg_both_core_gfxe_c0;
3889185e988SLen Brown 	long long gfx_rc6_ms;
38927d47356SLen Brown 	unsigned int gfx_mhz;
390b4b91569SRafael Antognolli 	unsigned int gfx_act_mhz;
391c98d5d94SLen Brown 	unsigned int package_id;
3927c2ccc50SChen Yu 	unsigned long long energy_pkg;	/* MSR_PKG_ENERGY_STATUS */
3937c2ccc50SChen Yu 	unsigned long long energy_dram;	/* MSR_DRAM_ENERGY_STATUS */
3947c2ccc50SChen Yu 	unsigned long long energy_cores;	/* MSR_PP0_ENERGY_STATUS */
3957c2ccc50SChen Yu 	unsigned long long energy_gfx;	/* MSR_PP1_ENERGY_STATUS */
3967c2ccc50SChen Yu 	unsigned long long rapl_pkg_perf_status;	/* MSR_PKG_PERF_STATUS */
3977c2ccc50SChen Yu 	unsigned long long rapl_dram_perf_status;	/* MSR_DRAM_PERF_STATUS */
398889facbeSLen Brown 	unsigned int pkg_temp_c;
399a5c6d65dSLen Brown 	unsigned int uncore_mhz;
400678a3bd1SLen Brown 	unsigned long long counter[MAX_ADDED_COUNTERS];
401c98d5d94SLen Brown } *package_even, *package_odd;
402c98d5d94SLen Brown 
403c98d5d94SLen Brown #define ODD_COUNTERS thread_odd, core_odd, package_odd
404c98d5d94SLen Brown #define EVEN_COUNTERS thread_even, core_even, package_even
405c98d5d94SLen Brown 
40640f5cfe7SPrarit Bhargava #define GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no)	      \
40740f5cfe7SPrarit Bhargava 	((thread_base) +						      \
40840f5cfe7SPrarit Bhargava 	 ((pkg_no) *							      \
40940f5cfe7SPrarit Bhargava 	  topo.nodes_per_pkg * topo.cores_per_node * topo.threads_per_core) + \
41040f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node * topo.threads_per_core) +	      \
41140f5cfe7SPrarit Bhargava 	 ((core_no) * topo.threads_per_core) +				      \
41240f5cfe7SPrarit Bhargava 	 (thread_no))
41340f5cfe7SPrarit Bhargava 
41440f5cfe7SPrarit Bhargava #define GET_CORE(core_base, core_no, node_no, pkg_no)			\
41540f5cfe7SPrarit Bhargava 	((core_base) +							\
41640f5cfe7SPrarit Bhargava 	 ((pkg_no) *  topo.nodes_per_pkg * topo.cores_per_node) +	\
41740f5cfe7SPrarit Bhargava 	 ((node_no) * topo.cores_per_node) +				\
41840f5cfe7SPrarit Bhargava 	 (core_no))
41940f5cfe7SPrarit Bhargava 
420c98d5d94SLen Brown #define GET_PKG(pkg_base, pkg_no) (pkg_base + pkg_no)
421c98d5d94SLen Brown 
42287e15da9SChen Yu /*
42387e15da9SChen Yu  * The accumulated sum of MSR is defined as a monotonic
42487e15da9SChen Yu  * increasing MSR, it will be accumulated periodically,
42587e15da9SChen Yu  * despite its register's bit width.
42687e15da9SChen Yu  */
42787e15da9SChen Yu enum {
42887e15da9SChen Yu 	IDX_PKG_ENERGY,
42987e15da9SChen Yu 	IDX_DRAM_ENERGY,
43087e15da9SChen Yu 	IDX_PP0_ENERGY,
43187e15da9SChen Yu 	IDX_PP1_ENERGY,
43287e15da9SChen Yu 	IDX_PKG_PERF,
43387e15da9SChen Yu 	IDX_DRAM_PERF,
43487e15da9SChen Yu 	IDX_COUNT,
43587e15da9SChen Yu };
43687e15da9SChen Yu 
43787e15da9SChen Yu int get_msr_sum(int cpu, off_t offset, unsigned long long *msr);
43887e15da9SChen Yu 
43987e15da9SChen Yu struct msr_sum_array {
44087e15da9SChen Yu 	/* get_msr_sum() = sum + (get_msr() - last) */
44187e15da9SChen Yu 	struct {
44287e15da9SChen Yu 		/*The accumulated MSR value is updated by the timer */
44387e15da9SChen Yu 		unsigned long long sum;
44487e15da9SChen Yu 		/*The MSR footprint recorded in last timer */
44587e15da9SChen Yu 		unsigned long long last;
44687e15da9SChen Yu 	} entries[IDX_COUNT];
44787e15da9SChen Yu };
44887e15da9SChen Yu 
44987e15da9SChen Yu /* The percpu MSR sum array.*/
45087e15da9SChen Yu struct msr_sum_array *per_cpu_msr_sum;
45187e15da9SChen Yu 
45213a779deSCalvin Walton off_t idx_to_offset(int idx)
45387e15da9SChen Yu {
45413a779deSCalvin Walton 	off_t offset;
45587e15da9SChen Yu 
45687e15da9SChen Yu 	switch (idx) {
45787e15da9SChen Yu 	case IDX_PKG_ENERGY:
458301b1d3aSBas Nieuwenhuizen 		if (do_rapl & RAPL_AMD_F17H)
459301b1d3aSBas Nieuwenhuizen 			offset = MSR_PKG_ENERGY_STAT;
460301b1d3aSBas Nieuwenhuizen 		else
46187e15da9SChen Yu 			offset = MSR_PKG_ENERGY_STATUS;
46287e15da9SChen Yu 		break;
46387e15da9SChen Yu 	case IDX_DRAM_ENERGY:
46487e15da9SChen Yu 		offset = MSR_DRAM_ENERGY_STATUS;
46587e15da9SChen Yu 		break;
46687e15da9SChen Yu 	case IDX_PP0_ENERGY:
46787e15da9SChen Yu 		offset = MSR_PP0_ENERGY_STATUS;
46887e15da9SChen Yu 		break;
46987e15da9SChen Yu 	case IDX_PP1_ENERGY:
47087e15da9SChen Yu 		offset = MSR_PP1_ENERGY_STATUS;
47187e15da9SChen Yu 		break;
47287e15da9SChen Yu 	case IDX_PKG_PERF:
47387e15da9SChen Yu 		offset = MSR_PKG_PERF_STATUS;
47487e15da9SChen Yu 		break;
47587e15da9SChen Yu 	case IDX_DRAM_PERF:
47687e15da9SChen Yu 		offset = MSR_DRAM_PERF_STATUS;
47787e15da9SChen Yu 		break;
47887e15da9SChen Yu 	default:
47987e15da9SChen Yu 		offset = -1;
48087e15da9SChen Yu 	}
48187e15da9SChen Yu 	return offset;
48287e15da9SChen Yu }
48387e15da9SChen Yu 
48413a779deSCalvin Walton int offset_to_idx(off_t offset)
48587e15da9SChen Yu {
48687e15da9SChen Yu 	int idx;
48787e15da9SChen Yu 
48887e15da9SChen Yu 	switch (offset) {
48987e15da9SChen Yu 	case MSR_PKG_ENERGY_STATUS:
490301b1d3aSBas Nieuwenhuizen 	case MSR_PKG_ENERGY_STAT:
49187e15da9SChen Yu 		idx = IDX_PKG_ENERGY;
49287e15da9SChen Yu 		break;
49387e15da9SChen Yu 	case MSR_DRAM_ENERGY_STATUS:
49487e15da9SChen Yu 		idx = IDX_DRAM_ENERGY;
49587e15da9SChen Yu 		break;
49687e15da9SChen Yu 	case MSR_PP0_ENERGY_STATUS:
49787e15da9SChen Yu 		idx = IDX_PP0_ENERGY;
49887e15da9SChen Yu 		break;
49987e15da9SChen Yu 	case MSR_PP1_ENERGY_STATUS:
50087e15da9SChen Yu 		idx = IDX_PP1_ENERGY;
50187e15da9SChen Yu 		break;
50287e15da9SChen Yu 	case MSR_PKG_PERF_STATUS:
50387e15da9SChen Yu 		idx = IDX_PKG_PERF;
50487e15da9SChen Yu 		break;
50587e15da9SChen Yu 	case MSR_DRAM_PERF_STATUS:
50687e15da9SChen Yu 		idx = IDX_DRAM_PERF;
50787e15da9SChen Yu 		break;
50887e15da9SChen Yu 	default:
50987e15da9SChen Yu 		idx = -1;
51087e15da9SChen Yu 	}
51187e15da9SChen Yu 	return idx;
51287e15da9SChen Yu }
51387e15da9SChen Yu 
51487e15da9SChen Yu int idx_valid(int idx)
51587e15da9SChen Yu {
51687e15da9SChen Yu 	switch (idx) {
51787e15da9SChen Yu 	case IDX_PKG_ENERGY:
518301b1d3aSBas Nieuwenhuizen 		return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
51987e15da9SChen Yu 	case IDX_DRAM_ENERGY:
52087e15da9SChen Yu 		return do_rapl & RAPL_DRAM;
52187e15da9SChen Yu 	case IDX_PP0_ENERGY:
52287e15da9SChen Yu 		return do_rapl & RAPL_CORES_ENERGY_STATUS;
52387e15da9SChen Yu 	case IDX_PP1_ENERGY:
52487e15da9SChen Yu 		return do_rapl & RAPL_GFX;
52587e15da9SChen Yu 	case IDX_PKG_PERF:
52687e15da9SChen Yu 		return do_rapl & RAPL_PKG_PERF_STATUS;
52787e15da9SChen Yu 	case IDX_DRAM_PERF:
52887e15da9SChen Yu 		return do_rapl & RAPL_DRAM_PERF_STATUS;
52987e15da9SChen Yu 	default:
53087e15da9SChen Yu 		return 0;
53187e15da9SChen Yu 	}
53287e15da9SChen Yu }
5331b439f01SLen Brown 
534388e9c81SLen Brown struct sys_counters {
535678a3bd1SLen Brown 	unsigned int added_thread_counters;
536678a3bd1SLen Brown 	unsigned int added_core_counters;
537678a3bd1SLen Brown 	unsigned int added_package_counters;
538388e9c81SLen Brown 	struct msr_counter *tp;
539388e9c81SLen Brown 	struct msr_counter *cp;
540388e9c81SLen Brown 	struct msr_counter *pp;
541388e9c81SLen Brown } sys;
542388e9c81SLen Brown 
543c98d5d94SLen Brown struct system_summary {
544c98d5d94SLen Brown 	struct thread_data threads;
545c98d5d94SLen Brown 	struct core_data cores;
546c98d5d94SLen Brown 	struct pkg_data packages;
547388e9c81SLen Brown } average;
548c98d5d94SLen Brown 
5490e2d8f05SLen Brown struct cpu_topology {
5500e2d8f05SLen Brown 	int physical_package_id;
5516de68fe1SLen Brown 	int die_id;
5520e2d8f05SLen Brown 	int logical_cpu_id;
553ef605741SPrarit Bhargava 	int physical_node_id;
554ef605741SPrarit Bhargava 	int logical_node_id;	/* 0-based count within the package */
5550e2d8f05SLen Brown 	int physical_core_id;
5568cb48b32SPrarit Bhargava 	int thread_id;
5570e2d8f05SLen Brown 	cpu_set_t *put_ids;	/* Processing Unit/Thread IDs */
5580e2d8f05SLen Brown } *cpus;
559c98d5d94SLen Brown 
560c98d5d94SLen Brown struct topo_params {
561c98d5d94SLen Brown 	int num_packages;
5626de68fe1SLen Brown 	int num_die;
563103a8feaSLen Brown 	int num_cpus;
564c98d5d94SLen Brown 	int num_cores;
565c98d5d94SLen Brown 	int max_cpu_num;
566ef605741SPrarit Bhargava 	int max_node_num;
56770a9c6e8SPrarit Bhargava 	int nodes_per_pkg;
56870a9c6e8SPrarit Bhargava 	int cores_per_node;
56970a9c6e8SPrarit Bhargava 	int threads_per_core;
570c98d5d94SLen Brown } topo;
571103a8feaSLen Brown 
572c98d5d94SLen Brown struct timeval tv_even, tv_odd, tv_delta;
573103a8feaSLen Brown 
574562a2d37SLen Brown int *irq_column_2_cpu;		/* /proc/interrupts column numbers */
575562a2d37SLen Brown int *irqs_per_cpu;		/* indexed by cpu_num */
576562a2d37SLen Brown 
577c98d5d94SLen Brown void setup_all_buffers(void);
578103a8feaSLen Brown 
5791f81c5efSLen Brown char *sys_lpi_file;
5801f81c5efSLen Brown char *sys_lpi_file_sysfs = "/sys/devices/system/cpu/cpuidle/low_power_idle_system_residency_us";
5811f81c5efSLen Brown char *sys_lpi_file_debugfs = "/sys/kernel/debug/pmc_core/slp_s0_residency_usec";
5821f81c5efSLen Brown 
583c98d5d94SLen Brown int cpu_is_not_present(int cpu)
584d15cf7c1SLen Brown {
585c98d5d94SLen Brown 	return !CPU_ISSET_S(cpu, cpu_present_setsize, cpu_present_set);
586c98d5d94SLen Brown }
5871b439f01SLen Brown 
588c98d5d94SLen Brown /*
589c98d5d94SLen Brown  * run func(thread, core, package) in topology order
590c98d5d94SLen Brown  * skip non-present cpus
591c98d5d94SLen Brown  */
592c98d5d94SLen Brown 
593c98d5d94SLen Brown int for_all_cpus(int (func) (struct thread_data *, struct core_data *, struct pkg_data *),
594c98d5d94SLen Brown 		 struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base)
595c98d5d94SLen Brown {
59640f5cfe7SPrarit Bhargava 	int retval, pkg_no, core_no, thread_no, node_no;
597c98d5d94SLen Brown 
598c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
599df2f677dSLen Brown 		for (node_no = 0; node_no < topo.nodes_per_pkg; node_no++) {
60070a9c6e8SPrarit Bhargava 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
6011b439f01SLen Brown 				for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
602c98d5d94SLen Brown 					struct thread_data *t;
603c98d5d94SLen Brown 					struct core_data *c;
604c98d5d94SLen Brown 					struct pkg_data *p;
605c98d5d94SLen Brown 
6061b439f01SLen Brown 					t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
607c98d5d94SLen Brown 
608c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
609c98d5d94SLen Brown 						continue;
610c98d5d94SLen Brown 
6111b439f01SLen Brown 					c = GET_CORE(core_base, core_no, node_no, pkg_no);
612c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
613c98d5d94SLen Brown 
614c98d5d94SLen Brown 					retval = func(t, c, p);
615c98d5d94SLen Brown 					if (retval)
616c98d5d94SLen Brown 						return retval;
617c98d5d94SLen Brown 				}
618c98d5d94SLen Brown 			}
619c98d5d94SLen Brown 		}
62040f5cfe7SPrarit Bhargava 	}
621d15cf7c1SLen Brown 	return 0;
622d15cf7c1SLen Brown }
623d15cf7c1SLen Brown 
62488c3281fSLen Brown int cpu_migrate(int cpu)
62588c3281fSLen Brown {
626c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
627c98d5d94SLen Brown 	CPU_SET_S(cpu, cpu_affinity_setsize, cpu_affinity_set);
628c98d5d94SLen Brown 	if (sched_setaffinity(0, cpu_affinity_setsize, cpu_affinity_set) == -1)
62988c3281fSLen Brown 		return -1;
63088c3281fSLen Brown 	else
63188c3281fSLen Brown 		return 0;
63288c3281fSLen Brown }
6331b439f01SLen Brown 
63436229897SLen Brown int get_msr_fd(int cpu)
635103a8feaSLen Brown {
636103a8feaSLen Brown 	char pathname[32];
637103a8feaSLen Brown 	int fd;
638103a8feaSLen Brown 
63936229897SLen Brown 	fd = fd_percpu[cpu];
64036229897SLen Brown 
64136229897SLen Brown 	if (fd)
64236229897SLen Brown 		return fd;
64336229897SLen Brown 
644103a8feaSLen Brown 	sprintf(pathname, "/dev/cpu/%d/msr", cpu);
645103a8feaSLen Brown 	fd = open(pathname, O_RDONLY);
64615aaa346SLen Brown 	if (fd < 0)
64798481e79SLen Brown 		err(-1, "%s open failed, try chown or chmod +r /dev/cpu/*/msr, or run as root", pathname);
648103a8feaSLen Brown 
64936229897SLen Brown 	fd_percpu[cpu] = fd;
65036229897SLen Brown 
65136229897SLen Brown 	return fd;
65236229897SLen Brown }
65336229897SLen Brown 
6542af4f9b8SLen Brown static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
6552af4f9b8SLen Brown {
6562af4f9b8SLen Brown 	return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);
6572af4f9b8SLen Brown }
6582af4f9b8SLen Brown 
6592af4f9b8SLen Brown static int perf_instr_count_open(int cpu_num)
6602af4f9b8SLen Brown {
6612af4f9b8SLen Brown 	struct perf_event_attr pea;
6622af4f9b8SLen Brown 	int fd;
6632af4f9b8SLen Brown 
6642af4f9b8SLen Brown 	memset(&pea, 0, sizeof(struct perf_event_attr));
6652af4f9b8SLen Brown 	pea.type = PERF_TYPE_HARDWARE;
6662af4f9b8SLen Brown 	pea.size = sizeof(struct perf_event_attr);
6672af4f9b8SLen Brown 	pea.config = PERF_COUNT_HW_INSTRUCTIONS;
6682af4f9b8SLen Brown 
6692af4f9b8SLen Brown 	/* counter for cpu_num, including user + kernel and all processes */
6702af4f9b8SLen Brown 	fd = perf_event_open(&pea, -1, cpu_num, -1, 0);
671164d7a96SLen Brown 	if (fd == -1) {
672164d7a96SLen Brown 		warn("cpu%d: perf instruction counter", cpu_num);
673164d7a96SLen Brown 		BIC_NOT_PRESENT(BIC_IPC);
674164d7a96SLen Brown 	}
6752af4f9b8SLen Brown 
6762af4f9b8SLen Brown 	return fd;
6772af4f9b8SLen Brown }
6782af4f9b8SLen Brown 
6792af4f9b8SLen Brown int get_instr_count_fd(int cpu)
6802af4f9b8SLen Brown {
6812af4f9b8SLen Brown 	if (fd_instr_count_percpu[cpu])
6822af4f9b8SLen Brown 		return fd_instr_count_percpu[cpu];
6832af4f9b8SLen Brown 
6842af4f9b8SLen Brown 	fd_instr_count_percpu[cpu] = perf_instr_count_open(cpu);
6852af4f9b8SLen Brown 
6862af4f9b8SLen Brown 	return fd_instr_count_percpu[cpu];
6872af4f9b8SLen Brown }
6882af4f9b8SLen Brown 
68936229897SLen Brown int get_msr(int cpu, off_t offset, unsigned long long *msr)
69036229897SLen Brown {
69136229897SLen Brown 	ssize_t retval;
69236229897SLen Brown 
69336229897SLen Brown 	retval = pread(get_msr_fd(cpu), msr, sizeof(*msr), offset);
69415aaa346SLen Brown 
69598481e79SLen Brown 	if (retval != sizeof *msr)
696cf4cbe53SLen Brown 		err(-1, "cpu%d: msr offset 0x%llx read failed", cpu, (unsigned long long)offset);
69715aaa346SLen Brown 
69815aaa346SLen Brown 	return 0;
699103a8feaSLen Brown }
700103a8feaSLen Brown 
701dd778a5eSLen Brown #define MAX_DEFERRED 16
7020fc521bcSZephaniah E. Loss-Cutler-Hull char *deferred_add_names[MAX_DEFERRED];
703dd778a5eSLen Brown char *deferred_skip_names[MAX_DEFERRED];
7040fc521bcSZephaniah E. Loss-Cutler-Hull int deferred_add_index;
705dd778a5eSLen Brown int deferred_skip_index;
706dd778a5eSLen Brown 
707dd778a5eSLen Brown /*
708dd778a5eSLen Brown  * HIDE_LIST - hide this list of counters, show the rest [default]
709dd778a5eSLen Brown  * SHOW_LIST - show this list of counters, hide the rest
710dd778a5eSLen Brown  */
711dd778a5eSLen Brown enum show_hide_mode { SHOW_LIST, HIDE_LIST } global_show_hide_mode = HIDE_LIST;
712dd778a5eSLen Brown 
713dd778a5eSLen Brown void help(void)
714dd778a5eSLen Brown {
715dd778a5eSLen Brown 	fprintf(outf,
716dd778a5eSLen Brown 		"Usage: turbostat [OPTIONS][(--interval seconds) | COMMAND ...]\n"
717dd778a5eSLen Brown 		"\n"
718dd778a5eSLen Brown 		"Turbostat forks the specified COMMAND and prints statistics\n"
719dd778a5eSLen Brown 		"when COMMAND completes.\n"
720dd778a5eSLen Brown 		"If no COMMAND is specified, turbostat wakes every 5-seconds\n"
721dd778a5eSLen Brown 		"to print statistics, until interrupted.\n"
722cc481650SNathan Ciobanu 		"  -a, --add	add a counter\n"
723dd778a5eSLen Brown 		"		  eg. --add msr0x10,u64,cpu,delta,MY_TSC\n"
724cc481650SNathan Ciobanu 		"  -c, --cpu	cpu-set	limit output to summary plus cpu-set:\n"
725dd778a5eSLen Brown 		"		  {core | package | j,k,l..m,n-p }\n"
7269ce80578SNathan Ciobanu 		"  -d, --debug	displays usec, Time_Of_Day_Seconds and more debugging\n"
7279ce80578SNathan Ciobanu 		"  -D, --Dump	displays the raw counter values\n"
7289ce80578SNathan Ciobanu 		"  -e, --enable	[all | column]\n"
7299ce80578SNathan Ciobanu 		"		shows all or the specified disabled column\n"
7309ce80578SNathan Ciobanu 		"  -H, --hide [column|column,column,...]\n"
7319ce80578SNathan Ciobanu 		"		hide the specified column(s)\n"
732cc481650SNathan Ciobanu 		"  -i, --interval sec.subsec\n"
733cc481650SNathan Ciobanu 		"		Override default 5-second measurement interval\n"
7349ce80578SNathan Ciobanu 		"  -J, --Joules	displays energy in Joules instead of Watts\n"
735cc481650SNathan Ciobanu 		"  -l, --list	list column headers only\n"
736cc481650SNathan Ciobanu 		"  -n, --num_iterations num\n"
737cc481650SNathan Ciobanu 		"		number of the measurement iterations\n"
738c7e399f8SZephaniah E. Loss-Cutler-Hull 		"  -N, --header_iterations num\n"
739c7e399f8SZephaniah E. Loss-Cutler-Hull 		"		print header every num iterations\n"
740cc481650SNathan Ciobanu 		"  -o, --out file\n"
741cc481650SNathan Ciobanu 		"		create or truncate \"file\" for all output\n"
742cc481650SNathan Ciobanu 		"  -q, --quiet	skip decoding system configuration header\n"
7439ce80578SNathan Ciobanu 		"  -s, --show [column|column,column,...]\n"
7449ce80578SNathan Ciobanu 		"		show only the specified column(s)\n"
7459ce80578SNathan Ciobanu 		"  -S, --Summary\n"
7469ce80578SNathan Ciobanu 		"		limits output to 1-line system summary per interval\n"
7479ce80578SNathan Ciobanu 		"  -T, --TCC temperature\n"
7489ce80578SNathan Ciobanu 		"		sets the Thermal Control Circuit temperature in\n"
7499ce80578SNathan Ciobanu 		"		  degrees Celsius\n"
750cc481650SNathan Ciobanu 		"  -h, --help	print this help message\n"
7511b439f01SLen Brown 		"  -v, --version	print version information\n" "\n" "For more help, run \"man turbostat\"\n");
752dd778a5eSLen Brown }
753dd778a5eSLen Brown 
754812db3f7SLen Brown /*
755812db3f7SLen Brown  * bic_lookup
756812db3f7SLen Brown  * for all the strings in comma separate name_list,
757812db3f7SLen Brown  * set the approprate bit in return value.
758812db3f7SLen Brown  */
759dd778a5eSLen Brown unsigned long long bic_lookup(char *name_list, enum show_hide_mode mode)
760812db3f7SLen Brown {
7619878bf7aSLen Brown 	unsigned int i;
762812db3f7SLen Brown 	unsigned long long retval = 0;
763812db3f7SLen Brown 
764812db3f7SLen Brown 	while (name_list) {
765812db3f7SLen Brown 		char *comma;
766812db3f7SLen Brown 
767812db3f7SLen Brown 		comma = strchr(name_list, ',');
768812db3f7SLen Brown 
769812db3f7SLen Brown 		if (comma)
770812db3f7SLen Brown 			*comma = '\0';
771812db3f7SLen Brown 
772812db3f7SLen Brown 		for (i = 0; i < MAX_BIC; ++i) {
773812db3f7SLen Brown 			if (!strcmp(name_list, bic[i].name)) {
774812db3f7SLen Brown 				retval |= (1ULL << i);
775812db3f7SLen Brown 				break;
776812db3f7SLen Brown 			}
7775dc241f2SLen Brown 			if (!strcmp(name_list, "all")) {
7785dc241f2SLen Brown 				retval |= ~0;
7795dc241f2SLen Brown 				break;
7805dc241f2SLen Brown 			} else if (!strcmp(name_list, "topology")) {
7815dc241f2SLen Brown 				retval |= BIC_TOPOLOGY;
7825dc241f2SLen Brown 				break;
7835dc241f2SLen Brown 			} else if (!strcmp(name_list, "power")) {
7845dc241f2SLen Brown 				retval |= BIC_THERMAL_PWR;
7855dc241f2SLen Brown 				break;
7865dc241f2SLen Brown 			} else if (!strcmp(name_list, "idle")) {
7875dc241f2SLen Brown 				retval |= BIC_IDLE;
7885dc241f2SLen Brown 				break;
7895dc241f2SLen Brown 			} else if (!strcmp(name_list, "frequency")) {
7905dc241f2SLen Brown 				retval |= BIC_FREQUENCY;
7915dc241f2SLen Brown 				break;
7925dc241f2SLen Brown 			} else if (!strcmp(name_list, "other")) {
7935dc241f2SLen Brown 				retval |= BIC_OTHER;
7945dc241f2SLen Brown 				break;
7955dc241f2SLen Brown 			}
7965dc241f2SLen Brown 
797812db3f7SLen Brown 		}
798812db3f7SLen Brown 		if (i == MAX_BIC) {
799dd778a5eSLen Brown 			if (mode == SHOW_LIST) {
8000fc521bcSZephaniah E. Loss-Cutler-Hull 				deferred_add_names[deferred_add_index++] = name_list;
8010fc521bcSZephaniah E. Loss-Cutler-Hull 				if (deferred_add_index >= MAX_DEFERRED) {
8020fc521bcSZephaniah E. Loss-Cutler-Hull 					fprintf(stderr, "More than max %d un-recognized --add options '%s'\n",
8030fc521bcSZephaniah E. Loss-Cutler-Hull 						MAX_DEFERRED, name_list);
8040fc521bcSZephaniah E. Loss-Cutler-Hull 					help();
8050fc521bcSZephaniah E. Loss-Cutler-Hull 					exit(1);
806812db3f7SLen Brown 				}
8070fc521bcSZephaniah E. Loss-Cutler-Hull 			} else {
808dd778a5eSLen Brown 				deferred_skip_names[deferred_skip_index++] = name_list;
809dd778a5eSLen Brown 				if (debug)
810dd778a5eSLen Brown 					fprintf(stderr, "deferred \"%s\"\n", name_list);
811dd778a5eSLen Brown 				if (deferred_skip_index >= MAX_DEFERRED) {
812dd778a5eSLen Brown 					fprintf(stderr, "More than max %d un-recognized --skip options '%s'\n",
813dd778a5eSLen Brown 						MAX_DEFERRED, name_list);
814dd778a5eSLen Brown 					help();
815dd778a5eSLen Brown 					exit(1);
816dd778a5eSLen Brown 				}
817dd778a5eSLen Brown 			}
8180fc521bcSZephaniah E. Loss-Cutler-Hull 		}
819812db3f7SLen Brown 
820812db3f7SLen Brown 		name_list = comma;
821812db3f7SLen Brown 		if (name_list)
822812db3f7SLen Brown 			name_list++;
823812db3f7SLen Brown 
824812db3f7SLen Brown 	}
825812db3f7SLen Brown 	return retval;
826812db3f7SLen Brown }
827fc04cc67SLen Brown 
828c8ade361SLen Brown void print_header(char *delim)
829103a8feaSLen Brown {
830388e9c81SLen Brown 	struct msr_counter *mp;
8316168c2e0SLen Brown 	int printed = 0;
832388e9c81SLen Brown 
8333f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC))
8343f44a5c6SLen Brown 		outp += sprintf(outp, "%susec", (printed++ ? delim : ""));
8353f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
8363f44a5c6SLen Brown 		outp += sprintf(outp, "%sTime_Of_Day_Seconds", (printed++ ? delim : ""));
837812db3f7SLen Brown 	if (DO_BIC(BIC_Package))
8386168c2e0SLen Brown 		outp += sprintf(outp, "%sPackage", (printed++ ? delim : ""));
8396de68fe1SLen Brown 	if (DO_BIC(BIC_Die))
8406de68fe1SLen Brown 		outp += sprintf(outp, "%sDie", (printed++ ? delim : ""));
84101235041SPrarit Bhargava 	if (DO_BIC(BIC_Node))
84201235041SPrarit Bhargava 		outp += sprintf(outp, "%sNode", (printed++ ? delim : ""));
843812db3f7SLen Brown 	if (DO_BIC(BIC_Core))
8446168c2e0SLen Brown 		outp += sprintf(outp, "%sCore", (printed++ ? delim : ""));
845812db3f7SLen Brown 	if (DO_BIC(BIC_CPU))
8466168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU", (printed++ ? delim : ""));
8474c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
8484c2122d4SLen Brown 		outp += sprintf(outp, "%sAPIC", (printed++ ? delim : ""));
8494c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
8504c2122d4SLen Brown 		outp += sprintf(outp, "%sX2APIC", (printed++ ? delim : ""));
851812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
8526168c2e0SLen Brown 		outp += sprintf(outp, "%sAvg_MHz", (printed++ ? delim : ""));
853812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
8546168c2e0SLen Brown 		outp += sprintf(outp, "%sBusy%%", (printed++ ? delim : ""));
855812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz))
8566168c2e0SLen Brown 		outp += sprintf(outp, "%sBzy_MHz", (printed++ ? delim : ""));
857812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
8586168c2e0SLen Brown 		outp += sprintf(outp, "%sTSC_MHz", (printed++ ? delim : ""));
8591cc21f7bSLen Brown 
8602af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
8612af4f9b8SLen Brown 		outp += sprintf(outp, "%sIPC", (printed++ ? delim : ""));
8622af4f9b8SLen Brown 
8630de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
8640de6c0dfSLen Brown 		if (sums_need_wide_columns)
8656168c2e0SLen Brown 			outp += sprintf(outp, "%s     IRQ", (printed++ ? delim : ""));
8660de6c0dfSLen Brown 		else
8676168c2e0SLen Brown 			outp += sprintf(outp, "%sIRQ", (printed++ ? delim : ""));
8680de6c0dfSLen Brown 	}
8690de6c0dfSLen Brown 
870812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
8716168c2e0SLen Brown 		outp += sprintf(outp, "%sSMI", (printed++ ? delim : ""));
8721cc21f7bSLen Brown 
873388e9c81SLen Brown 	for (mp = sys.tp; mp; mp = mp->next) {
874dd778a5eSLen Brown 
875388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
876388e9c81SLen Brown 			if (mp->width == 64)
877dd778a5eSLen Brown 				outp += sprintf(outp, "%s%18.18s", (printed++ ? delim : ""), mp->name);
878388e9c81SLen Brown 			else
879dd778a5eSLen Brown 				outp += sprintf(outp, "%s%10.10s", (printed++ ? delim : ""), mp->name);
880388e9c81SLen Brown 		} else {
8810de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
882dd778a5eSLen Brown 				outp += sprintf(outp, "%s%8s", (printed++ ? delim : ""), mp->name);
8830de6c0dfSLen Brown 			else
884dd778a5eSLen Brown 				outp += sprintf(outp, "%s%s", (printed++ ? delim : ""), mp->name);
885388e9c81SLen Brown 		}
886388e9c81SLen Brown 	}
887388e9c81SLen Brown 
88841618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
8896168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c1", (printed++ ? delim : ""));
890562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
8916168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c3", (printed++ ? delim : ""));
892812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
8936168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c6", (printed++ ? delim : ""));
894812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
8956168c2e0SLen Brown 		outp += sprintf(outp, "%sCPU%%c7", (printed++ ? delim : ""));
896678a3bd1SLen Brown 
8970539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
8986168c2e0SLen Brown 		outp += sprintf(outp, "%sMod%%c6", (printed++ ? delim : ""));
899678a3bd1SLen Brown 
900812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
9016168c2e0SLen Brown 		outp += sprintf(outp, "%sCoreTmp", (printed++ ? delim : ""));
902388e9c81SLen Brown 
903eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
904eae97e05SChen Yu 		outp += sprintf(outp, "%sCoreThr", (printed++ ? delim : ""));
905eae97e05SChen Yu 
9069392bd98SCalvin Walton 	if (do_rapl && !rapl_joules) {
9079392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
9089392bd98SCalvin Walton 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
9099392bd98SCalvin Walton 	} else if (do_rapl && rapl_joules) {
9109392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
9119392bd98SCalvin Walton 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
9129392bd98SCalvin Walton 	}
9139392bd98SCalvin Walton 
914388e9c81SLen Brown 	for (mp = sys.cp; mp; mp = mp->next) {
915388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
916388e9c81SLen Brown 			if (mp->width == 64)
917c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
918388e9c81SLen Brown 			else
919c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
920388e9c81SLen Brown 		} else {
9210de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
9220de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
9230de6c0dfSLen Brown 			else
924c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
925388e9c81SLen Brown 		}
926388e9c81SLen Brown 	}
927388e9c81SLen Brown 
928812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
9296168c2e0SLen Brown 		outp += sprintf(outp, "%sPkgTmp", (printed++ ? delim : ""));
930889facbeSLen Brown 
931812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
9326168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%rc6", (printed++ ? delim : ""));
933fdf676e5SLen Brown 
934812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
9356168c2e0SLen Brown 		outp += sprintf(outp, "%sGFXMHz", (printed++ ? delim : ""));
93627d47356SLen Brown 
937b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
938b4b91569SRafael Antognolli 		outp += sprintf(outp, "%sGFXAMHz", (printed++ ? delim : ""));
939b4b91569SRafael Antognolli 
940a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
9416168c2e0SLen Brown 		outp += sprintf(outp, "%sTotl%%C0", (printed++ ? delim : ""));
942a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
9436168c2e0SLen Brown 		outp += sprintf(outp, "%sAny%%C0", (printed++ ? delim : ""));
944a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
9456168c2e0SLen Brown 		outp += sprintf(outp, "%sGFX%%C0", (printed++ ? delim : ""));
946a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
9476168c2e0SLen Brown 		outp += sprintf(outp, "%sCPUGFX%%", (printed++ ? delim : ""));
9480b2bb692SLen Brown 
9490f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
9506168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc2", (printed++ ? delim : ""));
9510f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
9526168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc3", (printed++ ? delim : ""));
9530f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
9546168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc6", (printed++ ? delim : ""));
9550f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
9566168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc7", (printed++ ? delim : ""));
9570f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
9586168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc8", (printed++ ? delim : ""));
9590f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
9606168c2e0SLen Brown 		outp += sprintf(outp, "%sPkg%%pc9", (printed++ ? delim : ""));
9610f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
9626168c2e0SLen Brown 		outp += sprintf(outp, "%sPk%%pc10", (printed++ ? delim : ""));
963be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
964be0e54c4SLen Brown 		outp += sprintf(outp, "%sCPU%%LPI", (printed++ ? delim : ""));
965be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
966be0e54c4SLen Brown 		outp += sprintf(outp, "%sSYS%%LPI", (printed++ ? delim : ""));
967103a8feaSLen Brown 
9685c56be9aSDirk Brandewie 	if (do_rapl && !rapl_joules) {
969812db3f7SLen Brown 		if (DO_BIC(BIC_PkgWatt))
9706168c2e0SLen Brown 			outp += sprintf(outp, "%sPkgWatt", (printed++ ? delim : ""));
9719392bd98SCalvin Walton 		if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9726168c2e0SLen Brown 			outp += sprintf(outp, "%sCorWatt", (printed++ ? delim : ""));
973812db3f7SLen Brown 		if (DO_BIC(BIC_GFXWatt))
9746168c2e0SLen Brown 			outp += sprintf(outp, "%sGFXWatt", (printed++ ? delim : ""));
975812db3f7SLen Brown 		if (DO_BIC(BIC_RAMWatt))
9766168c2e0SLen Brown 			outp += sprintf(outp, "%sRAMWatt", (printed++ ? delim : ""));
977812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
9786168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
979812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
9806168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
981d7899447SLen Brown 	} else if (do_rapl && rapl_joules) {
982812db3f7SLen Brown 		if (DO_BIC(BIC_Pkg_J))
9836168c2e0SLen Brown 			outp += sprintf(outp, "%sPkg_J", (printed++ ? delim : ""));
9849392bd98SCalvin Walton 		if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
9856168c2e0SLen Brown 			outp += sprintf(outp, "%sCor_J", (printed++ ? delim : ""));
986812db3f7SLen Brown 		if (DO_BIC(BIC_GFX_J))
9876168c2e0SLen Brown 			outp += sprintf(outp, "%sGFX_J", (printed++ ? delim : ""));
988812db3f7SLen Brown 		if (DO_BIC(BIC_RAM_J))
9896168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_J", (printed++ ? delim : ""));
990812db3f7SLen Brown 		if (DO_BIC(BIC_PKG__))
9916168c2e0SLen Brown 			outp += sprintf(outp, "%sPKG_%%", (printed++ ? delim : ""));
992812db3f7SLen Brown 		if (DO_BIC(BIC_RAM__))
9936168c2e0SLen Brown 			outp += sprintf(outp, "%sRAM_%%", (printed++ ? delim : ""));
9945c56be9aSDirk Brandewie 	}
995a5c6d65dSLen Brown 	if (DO_BIC(BIC_UNCORE_MHZ))
996a5c6d65dSLen Brown 		outp += sprintf(outp, "%sUncMHz", (printed++ ? delim : ""));
997a5c6d65dSLen Brown 
998388e9c81SLen Brown 	for (mp = sys.pp; mp; mp = mp->next) {
999388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1000388e9c81SLen Brown 			if (mp->width == 64)
1001c8ade361SLen Brown 				outp += sprintf(outp, "%s%18.18s", delim, mp->name);
1002388e9c81SLen Brown 			else
1003c8ade361SLen Brown 				outp += sprintf(outp, "%s%10.10s", delim, mp->name);
1004388e9c81SLen Brown 		} else {
10050de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
10060de6c0dfSLen Brown 				outp += sprintf(outp, "%s%8s", delim, mp->name);
10070de6c0dfSLen Brown 			else
1008c8ade361SLen Brown 				outp += sprintf(outp, "%s%s", delim, mp->name);
1009388e9c81SLen Brown 		}
1010388e9c81SLen Brown 	}
1011388e9c81SLen Brown 
1012c98d5d94SLen Brown 	outp += sprintf(outp, "\n");
1013103a8feaSLen Brown }
1014103a8feaSLen Brown 
10151b439f01SLen Brown int dump_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1016103a8feaSLen Brown {
1017388e9c81SLen Brown 	int i;
1018388e9c81SLen Brown 	struct msr_counter *mp;
1019388e9c81SLen Brown 
10203b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "t %p, c %p, p %p\n", t, c, p);
1021c98d5d94SLen Brown 
1022c98d5d94SLen Brown 	if (t) {
10231b439f01SLen Brown 		outp += sprintf(outp, "CPU: %d flags 0x%x\n", t->cpu_id, t->flags);
10243b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "TSC: %016llX\n", t->tsc);
10253b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "aperf: %016llX\n", t->aperf);
10263b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "mperf: %016llX\n", t->mperf);
10273b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c1: %016llX\n", t->c1);
10286886fee4SLen Brown 
10292af4f9b8SLen Brown 		if (DO_BIC(BIC_IPC))
10302af4f9b8SLen Brown 			outp += sprintf(outp, "IPC: %lld\n", t->instr_count);
10312af4f9b8SLen Brown 
1032812db3f7SLen Brown 		if (DO_BIC(BIC_IRQ))
10330de6c0dfSLen Brown 			outp += sprintf(outp, "IRQ: %lld\n", t->irq_count);
1034812db3f7SLen Brown 		if (DO_BIC(BIC_SMI))
1035218f0e8dSLen Brown 			outp += sprintf(outp, "SMI: %d\n", t->smi_count);
1036388e9c81SLen Brown 
1037388e9c81SLen Brown 		for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
10381b439f01SLen Brown 			outp += sprintf(outp, "tADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, t->counter[i]);
1039388e9c81SLen Brown 		}
1040103a8feaSLen Brown 	}
1041103a8feaSLen Brown 
1042c98d5d94SLen Brown 	if (c) {
10433b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "core: %d\n", c->core_id);
10443b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c3: %016llX\n", c->c3);
10453b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c6: %016llX\n", c->c6);
10463b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "c7: %016llX\n", c->c7);
10473b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "DTS: %dC\n", c->core_temp_c);
1048eae97e05SChen Yu 		outp += sprintf(outp, "cpu_throt_count: %016llX\n", c->core_throt_cnt);
10499392bd98SCalvin Walton 		outp += sprintf(outp, "Joules: %0X\n", c->core_energy);
1050388e9c81SLen Brown 
1051388e9c81SLen Brown 		for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
10521b439f01SLen Brown 			outp += sprintf(outp, "cADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, c->counter[i]);
1053388e9c81SLen Brown 		}
10540539ba11SLen Brown 		outp += sprintf(outp, "mc6_us: %016llX\n", c->mc6_us);
1055c98d5d94SLen Brown 	}
1056103a8feaSLen Brown 
1057c98d5d94SLen Brown 	if (p) {
10583b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "package: %d\n", p->package_id);
10590b2bb692SLen Brown 
10600b2bb692SLen Brown 		outp += sprintf(outp, "Weighted cores: %016llX\n", p->pkg_wtd_core_c0);
10610b2bb692SLen Brown 		outp += sprintf(outp, "Any cores: %016llX\n", p->pkg_any_core_c0);
10620b2bb692SLen Brown 		outp += sprintf(outp, "Any GFX: %016llX\n", p->pkg_any_gfxe_c0);
10630b2bb692SLen Brown 		outp += sprintf(outp, "CPU + GFX: %016llX\n", p->pkg_both_core_gfxe_c0);
10640b2bb692SLen Brown 
10653b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc2: %016llX\n", p->pc2);
10660f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc3))
10673b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc3: %016llX\n", p->pc3);
10680f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc6))
10693b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc6: %016llX\n", p->pc6);
10700f47c08dSLen Brown 		if (DO_BIC(BIC_Pkgpc7))
10713b4d5c7fSAndy Shevchenko 			outp += sprintf(outp, "pc7: %016llX\n", p->pc7);
10723b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc8: %016llX\n", p->pc8);
10733b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc9: %016llX\n", p->pc9);
10743b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "pc10: %016llX\n", p->pc10);
1075be0e54c4SLen Brown 		outp += sprintf(outp, "cpu_lpi: %016llX\n", p->cpu_lpi);
1076be0e54c4SLen Brown 		outp += sprintf(outp, "sys_lpi: %016llX\n", p->sys_lpi);
10777c2ccc50SChen Yu 		outp += sprintf(outp, "Joules PKG: %0llX\n", p->energy_pkg);
10787c2ccc50SChen Yu 		outp += sprintf(outp, "Joules COR: %0llX\n", p->energy_cores);
10797c2ccc50SChen Yu 		outp += sprintf(outp, "Joules GFX: %0llX\n", p->energy_gfx);
10807c2ccc50SChen Yu 		outp += sprintf(outp, "Joules RAM: %0llX\n", p->energy_dram);
10811b439f01SLen Brown 		outp += sprintf(outp, "Throttle PKG: %0llX\n", p->rapl_pkg_perf_status);
10821b439f01SLen Brown 		outp += sprintf(outp, "Throttle RAM: %0llX\n", p->rapl_dram_perf_status);
10833b4d5c7fSAndy Shevchenko 		outp += sprintf(outp, "PTM: %dC\n", p->pkg_temp_c);
1084388e9c81SLen Brown 
1085388e9c81SLen Brown 		for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
10861b439f01SLen Brown 			outp += sprintf(outp, "pADDED [%d] msr0x%x: %08llX\n", i, mp->msr_num, p->counter[i]);
1087388e9c81SLen Brown 		}
1088c98d5d94SLen Brown 	}
10893b4d5c7fSAndy Shevchenko 
10903b4d5c7fSAndy Shevchenko 	outp += sprintf(outp, "\n");
10913b4d5c7fSAndy Shevchenko 
1092c98d5d94SLen Brown 	return 0;
1093103a8feaSLen Brown }
1094103a8feaSLen Brown 
1095e23da037SLen Brown /*
1096e23da037SLen Brown  * column formatting convention & formats
1097e23da037SLen Brown  */
10981b439f01SLen Brown int format_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1099103a8feaSLen Brown {
1100008d396eSLen Brown 	double interval_float, tsc;
1101fc04cc67SLen Brown 	char *fmt8;
1102388e9c81SLen Brown 	int i;
1103388e9c81SLen Brown 	struct msr_counter *mp;
11046168c2e0SLen Brown 	char *delim = "\t";
11056168c2e0SLen Brown 	int printed = 0;
1106103a8feaSLen Brown 
1107c98d5d94SLen Brown 	/* if showing only 1st thread in core and this isn't one, bail out */
1108c98d5d94SLen Brown 	if (show_core_only && !(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1109c98d5d94SLen Brown 		return 0;
1110c98d5d94SLen Brown 
1111c98d5d94SLen Brown 	/* if showing only 1st thread in pkg and this isn't one, bail out */
1112c98d5d94SLen Brown 	if (show_pkg_only && !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1113c98d5d94SLen Brown 		return 0;
1114c98d5d94SLen Brown 
11151ef7d21aSLen Brown 	/*if not summary line and --cpu is used */
11161b439f01SLen Brown 	if ((t != &average.threads) && (cpu_subset && !CPU_ISSET_S(t->cpu_id, cpu_subset_size, cpu_subset)))
11171ef7d21aSLen Brown 		return 0;
11181ef7d21aSLen Brown 
11193f44a5c6SLen Brown 	if (DO_BIC(BIC_USEC)) {
1120f4fdf2b4SLen Brown 		/* on each row, print how many usec each timestamp took to gather */
1121f4fdf2b4SLen Brown 		struct timeval tv;
1122f4fdf2b4SLen Brown 
1123f4fdf2b4SLen Brown 		timersub(&t->tv_end, &t->tv_begin, &tv);
1124f4fdf2b4SLen Brown 		outp += sprintf(outp, "%5ld\t", tv.tv_sec * 1000000 + tv.tv_usec);
1125f4fdf2b4SLen Brown 	}
1126f4fdf2b4SLen Brown 
11273f44a5c6SLen Brown 	/* Time_Of_Day_Seconds: on each row, print sec.usec last timestamp taken */
11283f44a5c6SLen Brown 	if (DO_BIC(BIC_TOD))
11293f44a5c6SLen Brown 		outp += sprintf(outp, "%10ld.%06ld\t", t->tv_end.tv_sec, t->tv_end.tv_usec);
11303f44a5c6SLen Brown 
1131d4794f25SYazen Ghannam 	interval_float = t->tv_delta.tv_sec + t->tv_delta.tv_usec / 1000000.0;
1132103a8feaSLen Brown 
1133008d396eSLen Brown 	tsc = t->tsc * tsc_tweak;
1134008d396eSLen Brown 
1135c98d5d94SLen Brown 	/* topo columns, print blanks on 1st (average) line */
1136c98d5d94SLen Brown 	if (t == &average.threads) {
1137812db3f7SLen Brown 		if (DO_BIC(BIC_Package))
11386168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11396de68fe1SLen Brown 		if (DO_BIC(BIC_Die))
11406de68fe1SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
114101235041SPrarit Bhargava 		if (DO_BIC(BIC_Node))
114201235041SPrarit Bhargava 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1143812db3f7SLen Brown 		if (DO_BIC(BIC_Core))
11446168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1145812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
11466168c2e0SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11474c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
11484c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11494c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
11504c2122d4SLen Brown 			outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1151103a8feaSLen Brown 	} else {
1152812db3f7SLen Brown 		if (DO_BIC(BIC_Package)) {
1153c98d5d94SLen Brown 			if (p)
11546168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->package_id);
1155c98d5d94SLen Brown 			else
11566168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1157c98d5d94SLen Brown 		}
11586de68fe1SLen Brown 		if (DO_BIC(BIC_Die)) {
11596de68fe1SLen Brown 			if (c)
11606de68fe1SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), cpus[t->cpu_id].die_id);
11616de68fe1SLen Brown 			else
11626de68fe1SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
11636de68fe1SLen Brown 		}
116401235041SPrarit Bhargava 		if (DO_BIC(BIC_Node)) {
116501235041SPrarit Bhargava 			if (t)
116601235041SPrarit Bhargava 				outp += sprintf(outp, "%s%d",
11671b439f01SLen Brown 						(printed++ ? delim : ""), cpus[t->cpu_id].physical_node_id);
116801235041SPrarit Bhargava 			else
11691b439f01SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
117001235041SPrarit Bhargava 		}
1171812db3f7SLen Brown 		if (DO_BIC(BIC_Core)) {
1172c98d5d94SLen Brown 			if (c)
11736168c2e0SLen Brown 				outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_id);
1174c98d5d94SLen Brown 			else
11756168c2e0SLen Brown 				outp += sprintf(outp, "%s-", (printed++ ? delim : ""));
1176c98d5d94SLen Brown 		}
1177812db3f7SLen Brown 		if (DO_BIC(BIC_CPU))
11786168c2e0SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->cpu_id);
11794c2122d4SLen Brown 		if (DO_BIC(BIC_APIC))
11804c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->apic_id);
11814c2122d4SLen Brown 		if (DO_BIC(BIC_X2APIC))
11824c2122d4SLen Brown 			outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->x2apic_id);
1183103a8feaSLen Brown 	}
1184fc04cc67SLen Brown 
1185812db3f7SLen Brown 	if (DO_BIC(BIC_Avg_MHz))
11861b439f01SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 / units * t->aperf / interval_float);
1187fc04cc67SLen Brown 
1188812db3f7SLen Brown 	if (DO_BIC(BIC_Busy))
11896168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->mperf / tsc);
1190103a8feaSLen Brown 
1191812db3f7SLen Brown 	if (DO_BIC(BIC_Bzy_MHz)) {
119221ed5574SLen Brown 		if (has_base_hz)
11931b439f01SLen Brown 			outp +=
11941b439f01SLen Brown 			    sprintf(outp, "%s%.0f", (printed++ ? delim : ""), base_hz / units * t->aperf / t->mperf);
119521ed5574SLen Brown 		else
11966168c2e0SLen Brown 			outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""),
1197008d396eSLen Brown 					tsc / units * t->aperf / t->mperf / interval_float);
119821ed5574SLen Brown 	}
1199103a8feaSLen Brown 
1200812db3f7SLen Brown 	if (DO_BIC(BIC_TSC_MHz))
12016168c2e0SLen Brown 		outp += sprintf(outp, "%s%.0f", (printed++ ? delim : ""), 1.0 * t->tsc / units / interval_float);
1202103a8feaSLen Brown 
12032af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
12042af4f9b8SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 1.0 * t->instr_count / t->aperf);
12052af4f9b8SLen Brown 
1206562a2d37SLen Brown 	/* IRQ */
12070de6c0dfSLen Brown 	if (DO_BIC(BIC_IRQ)) {
12080de6c0dfSLen Brown 		if (sums_need_wide_columns)
12096168c2e0SLen Brown 			outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->irq_count);
12100de6c0dfSLen Brown 		else
12116168c2e0SLen Brown 			outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->irq_count);
12120de6c0dfSLen Brown 	}
1213562a2d37SLen Brown 
12141cc21f7bSLen Brown 	/* SMI */
1215812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
12166168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), t->smi_count);
12171cc21f7bSLen Brown 
1218678a3bd1SLen Brown 	/* Added counters */
1219678a3bd1SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1220678a3bd1SLen Brown 		if (mp->format == FORMAT_RAW) {
1221678a3bd1SLen Brown 			if (mp->width == 32)
12221b439f01SLen Brown 				outp +=
12231b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)t->counter[i]);
1224678a3bd1SLen Brown 			else
12256168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), t->counter[i]);
1226678a3bd1SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
12270de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12286168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), t->counter[i]);
12290de6c0dfSLen Brown 			else
12306168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), t->counter[i]);
1231678a3bd1SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
123241618e63SLen Brown 			if (mp->type == COUNTER_USEC)
12331b439f01SLen Brown 				outp +=
12341b439f01SLen Brown 				    sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
12351b439f01SLen Brown 					    t->counter[i] / interval_float / 10000);
123641618e63SLen Brown 			else
12376168c2e0SLen Brown 				outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->counter[i] / tsc);
1238678a3bd1SLen Brown 		}
1239678a3bd1SLen Brown 	}
1240678a3bd1SLen Brown 
124141618e63SLen Brown 	/* C1 */
124241618e63SLen Brown 	if (DO_BIC(BIC_CPU_c1))
12436168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * t->c1 / tsc);
124441618e63SLen Brown 
1245c98d5d94SLen Brown 	/* print per-core data only for 1st thread in core */
1246c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1247c98d5d94SLen Brown 		goto done;
1248c98d5d94SLen Brown 
1249562855eeSLen Brown 	if (DO_BIC(BIC_CPU_c3))
12506168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c3 / tsc);
1251812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c6))
12526168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c6 / tsc);
1253812db3f7SLen Brown 	if (DO_BIC(BIC_CPU_c7))
12546168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->c7 / tsc);
1255c98d5d94SLen Brown 
12560539ba11SLen Brown 	/* Mod%c6 */
12570539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
12586168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->mc6_us / tsc);
12590539ba11SLen Brown 
1260812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp))
12616168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), c->core_temp_c);
1262889facbeSLen Brown 
1263eae97e05SChen Yu 	/* Core throttle count */
1264eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
1265eae97e05SChen Yu 		outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->core_throt_cnt);
1266eae97e05SChen Yu 
1267388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1268388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1269388e9c81SLen Brown 			if (mp->width == 32)
12701b439f01SLen Brown 				outp +=
12711b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)c->counter[i]);
1272388e9c81SLen Brown 			else
12736168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), c->counter[i]);
1274388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
12750de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
12766168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), c->counter[i]);
12770de6c0dfSLen Brown 			else
12786168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), c->counter[i]);
1279388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
12806168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * c->counter[i] / tsc);
1281388e9c81SLen Brown 		}
1282388e9c81SLen Brown 	}
1283388e9c81SLen Brown 
12849392bd98SCalvin Walton 	fmt8 = "%s%.2f";
12859392bd98SCalvin Walton 
12869392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && (do_rapl & RAPL_PER_CORE_ENERGY))
12871b439f01SLen Brown 		outp +=
12881b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units / interval_float);
12899392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && (do_rapl & RAPL_PER_CORE_ENERGY))
12909392bd98SCalvin Walton 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), c->core_energy * rapl_energy_units);
12919392bd98SCalvin Walton 
1292c98d5d94SLen Brown 	/* print per-package data only for 1st core in package */
1293c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1294c98d5d94SLen Brown 		goto done;
1295c98d5d94SLen Brown 
12960b2bb692SLen Brown 	/* PkgTmp */
1297812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp))
12986168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->pkg_temp_c);
1299889facbeSLen Brown 
1300fdf676e5SLen Brown 	/* GFXrc6 */
1301812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6)) {
1302ba3dec99SLen Brown 		if (p->gfx_rc6_ms == -1) {	/* detect GFX counter reset */
13036168c2e0SLen Brown 			outp += sprintf(outp, "%s**.**", (printed++ ? delim : ""));
13049185e988SLen Brown 		} else {
13056168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""),
13069185e988SLen Brown 					p->gfx_rc6_ms / 10.0 / interval_float);
13079185e988SLen Brown 		}
13089185e988SLen Brown 	}
1309fdf676e5SLen Brown 
131027d47356SLen Brown 	/* GFXMHz */
1311812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
13126168c2e0SLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_mhz);
131327d47356SLen Brown 
1314b4b91569SRafael Antognolli 	/* GFXACTMHz */
1315b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
1316b4b91569SRafael Antognolli 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->gfx_act_mhz);
1317b4b91569SRafael Antognolli 
13180b2bb692SLen Brown 	/* Totl%C0, Any%C0 GFX%C0 CPUGFX% */
1319a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
13206168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_wtd_core_c0 / tsc);
1321a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
13226168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_core_c0 / tsc);
1323a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
13246168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_any_gfxe_c0 / tsc);
1325a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
13266168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pkg_both_core_gfxe_c0 / tsc);
13270b2bb692SLen Brown 
13280f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
13296168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc2 / tsc);
13300f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
13316168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc3 / tsc);
13320f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
13336168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc6 / tsc);
13340f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
13356168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc7 / tsc);
13360f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
13376168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc8 / tsc);
13380f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
13396168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc9 / tsc);
13400f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
13416168c2e0SLen Brown 		outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->pc10 / tsc);
1342889facbeSLen Brown 
1343be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
13441b439f01SLen Brown 		outp +=
13451b439f01SLen Brown 		    sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->cpu_lpi / 1000000.0 / interval_float);
1346be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
13471b439f01SLen Brown 		outp +=
13481b439f01SLen Brown 		    sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->sys_lpi / 1000000.0 / interval_float);
1349be0e54c4SLen Brown 
1350812db3f7SLen Brown 	if (DO_BIC(BIC_PkgWatt))
13511b439f01SLen Brown 		outp +=
13521b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units / interval_float);
1353eae97e05SChen Yu 
13549392bd98SCalvin Walton 	if (DO_BIC(BIC_CorWatt) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13551b439f01SLen Brown 		outp +=
13561b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units / interval_float);
1357812db3f7SLen Brown 	if (DO_BIC(BIC_GFXWatt))
13581b439f01SLen Brown 		outp +=
13591b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units / interval_float);
1360812db3f7SLen Brown 	if (DO_BIC(BIC_RAMWatt))
13611b439f01SLen Brown 		outp +=
13621b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13631b439f01SLen Brown 			    p->energy_dram * rapl_dram_energy_units / interval_float);
1364812db3f7SLen Brown 	if (DO_BIC(BIC_Pkg_J))
13656168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_pkg * rapl_energy_units);
13669392bd98SCalvin Walton 	if (DO_BIC(BIC_Cor_J) && !(do_rapl & RAPL_PER_CORE_ENERGY))
13676168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_cores * rapl_energy_units);
1368812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_J))
13696168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_gfx * rapl_energy_units);
1370812db3f7SLen Brown 	if (DO_BIC(BIC_RAM_J))
13716168c2e0SLen Brown 		outp += sprintf(outp, fmt8, (printed++ ? delim : ""), p->energy_dram * rapl_dram_energy_units);
1372812db3f7SLen Brown 	if (DO_BIC(BIC_PKG__))
13731b439f01SLen Brown 		outp +=
13741b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13751b439f01SLen Brown 			    100.0 * p->rapl_pkg_perf_status * rapl_time_units / interval_float);
1376812db3f7SLen Brown 	if (DO_BIC(BIC_RAM__))
13771b439f01SLen Brown 		outp +=
13781b439f01SLen Brown 		    sprintf(outp, fmt8, (printed++ ? delim : ""),
13791b439f01SLen Brown 			    100.0 * p->rapl_dram_perf_status * rapl_time_units / interval_float);
1380a5c6d65dSLen Brown 	/* UncMHz */
1381a5c6d65dSLen Brown 	if (DO_BIC(BIC_UNCORE_MHZ))
1382a5c6d65dSLen Brown 		outp += sprintf(outp, "%s%d", (printed++ ? delim : ""), p->uncore_mhz);
1383812db3f7SLen Brown 
1384388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1385388e9c81SLen Brown 		if (mp->format == FORMAT_RAW) {
1386388e9c81SLen Brown 			if (mp->width == 32)
13871b439f01SLen Brown 				outp +=
13881b439f01SLen Brown 				    sprintf(outp, "%s0x%08x", (printed++ ? delim : ""), (unsigned int)p->counter[i]);
1389388e9c81SLen Brown 			else
13906168c2e0SLen Brown 				outp += sprintf(outp, "%s0x%016llx", (printed++ ? delim : ""), p->counter[i]);
1391388e9c81SLen Brown 		} else if (mp->format == FORMAT_DELTA) {
13920de6c0dfSLen Brown 			if ((mp->type == COUNTER_ITEMS) && sums_need_wide_columns)
13936168c2e0SLen Brown 				outp += sprintf(outp, "%s%8lld", (printed++ ? delim : ""), p->counter[i]);
13940de6c0dfSLen Brown 			else
13956168c2e0SLen Brown 				outp += sprintf(outp, "%s%lld", (printed++ ? delim : ""), p->counter[i]);
1396388e9c81SLen Brown 		} else if (mp->format == FORMAT_PERCENT) {
13976168c2e0SLen Brown 			outp += sprintf(outp, "%s%.2f", (printed++ ? delim : ""), 100.0 * p->counter[i] / tsc);
1398388e9c81SLen Brown 		}
1399388e9c81SLen Brown 	}
1400388e9c81SLen Brown 
1401c98d5d94SLen Brown done:
140294d6ab4bSLen Brown 	if (*(outp - 1) != '\n')
1403c98d5d94SLen Brown 		outp += sprintf(outp, "\n");
1404c98d5d94SLen Brown 
1405c98d5d94SLen Brown 	return 0;
1406103a8feaSLen Brown }
1407103a8feaSLen Brown 
1408b7d8c148SLen Brown void flush_output_stdout(void)
1409103a8feaSLen Brown {
1410b7d8c148SLen Brown 	FILE *filep;
1411b7d8c148SLen Brown 
1412b7d8c148SLen Brown 	if (outf == stderr)
1413b7d8c148SLen Brown 		filep = stdout;
1414b7d8c148SLen Brown 	else
1415b7d8c148SLen Brown 		filep = outf;
1416b7d8c148SLen Brown 
1417b7d8c148SLen Brown 	fputs(output_buffer, filep);
1418b7d8c148SLen Brown 	fflush(filep);
1419b7d8c148SLen Brown 
1420c98d5d94SLen Brown 	outp = output_buffer;
1421c98d5d94SLen Brown }
14221b439f01SLen Brown 
1423b7d8c148SLen Brown void flush_output_stderr(void)
1424c98d5d94SLen Brown {
1425b7d8c148SLen Brown 	fputs(output_buffer, outf);
1426b7d8c148SLen Brown 	fflush(outf);
1427c98d5d94SLen Brown 	outp = output_buffer;
1428c98d5d94SLen Brown }
14291b439f01SLen Brown 
1430c98d5d94SLen Brown void format_all_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1431c98d5d94SLen Brown {
1432c7e399f8SZephaniah E. Loss-Cutler-Hull 	static int count;
1433103a8feaSLen Brown 
1434c7e399f8SZephaniah E. Loss-Cutler-Hull 	if ((!count || (header_iterations && !(count % header_iterations))) || !summary_only)
1435c8ade361SLen Brown 		print_header("\t");
1436103a8feaSLen Brown 
14379d83601aSLen Brown 	format_counters(&average.threads, &average.cores, &average.packages);
1438103a8feaSLen Brown 
1439c7e399f8SZephaniah E. Loss-Cutler-Hull 	count++;
1440e23da037SLen Brown 
1441e23da037SLen Brown 	if (summary_only)
1442e23da037SLen Brown 		return;
1443e23da037SLen Brown 
1444c98d5d94SLen Brown 	for_all_cpus(format_counters, t, c, p);
1445103a8feaSLen Brown }
1446103a8feaSLen Brown 
1447889facbeSLen Brown #define DELTA_WRAP32(new, old)			\
14487c2ccc50SChen Yu 	old = ((((unsigned long long)new << 32) - ((unsigned long long)old << 32)) >> 32);
1449889facbeSLen Brown 
14501b439f01SLen Brown int delta_package(struct pkg_data *new, struct pkg_data *old)
1451103a8feaSLen Brown {
1452388e9c81SLen Brown 	int i;
1453388e9c81SLen Brown 	struct msr_counter *mp;
14540b2bb692SLen Brown 
1455a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
14560b2bb692SLen Brown 		old->pkg_wtd_core_c0 = new->pkg_wtd_core_c0 - old->pkg_wtd_core_c0;
1457a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
14580b2bb692SLen Brown 		old->pkg_any_core_c0 = new->pkg_any_core_c0 - old->pkg_any_core_c0;
1459a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
14600b2bb692SLen Brown 		old->pkg_any_gfxe_c0 = new->pkg_any_gfxe_c0 - old->pkg_any_gfxe_c0;
1461a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
14620b2bb692SLen Brown 		old->pkg_both_core_gfxe_c0 = new->pkg_both_core_gfxe_c0 - old->pkg_both_core_gfxe_c0;
1463a99d8730SLen Brown 
1464c98d5d94SLen Brown 	old->pc2 = new->pc2 - old->pc2;
14650f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1466c98d5d94SLen Brown 		old->pc3 = new->pc3 - old->pc3;
14670f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1468c98d5d94SLen Brown 		old->pc6 = new->pc6 - old->pc6;
14690f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1470c98d5d94SLen Brown 		old->pc7 = new->pc7 - old->pc7;
1471ca58710fSKristen Carlson Accardi 	old->pc8 = new->pc8 - old->pc8;
1472ca58710fSKristen Carlson Accardi 	old->pc9 = new->pc9 - old->pc9;
1473ca58710fSKristen Carlson Accardi 	old->pc10 = new->pc10 - old->pc10;
1474be0e54c4SLen Brown 	old->cpu_lpi = new->cpu_lpi - old->cpu_lpi;
1475be0e54c4SLen Brown 	old->sys_lpi = new->sys_lpi - old->sys_lpi;
1476889facbeSLen Brown 	old->pkg_temp_c = new->pkg_temp_c;
1477889facbeSLen Brown 
14789185e988SLen Brown 	/* flag an error when rc6 counter resets/wraps */
14799185e988SLen Brown 	if (old->gfx_rc6_ms > new->gfx_rc6_ms)
14809185e988SLen Brown 		old->gfx_rc6_ms = -1;
14819185e988SLen Brown 	else
1482fdf676e5SLen Brown 		old->gfx_rc6_ms = new->gfx_rc6_ms - old->gfx_rc6_ms;
14839185e988SLen Brown 
1484a5c6d65dSLen Brown 	old->uncore_mhz = new->uncore_mhz;
148527d47356SLen Brown 	old->gfx_mhz = new->gfx_mhz;
1486b4b91569SRafael Antognolli 	old->gfx_act_mhz = new->gfx_act_mhz;
148727d47356SLen Brown 
148887e15da9SChen Yu 	old->energy_pkg = new->energy_pkg - old->energy_pkg;
148987e15da9SChen Yu 	old->energy_cores = new->energy_cores - old->energy_cores;
149087e15da9SChen Yu 	old->energy_gfx = new->energy_gfx - old->energy_gfx;
149187e15da9SChen Yu 	old->energy_dram = new->energy_dram - old->energy_dram;
149287e15da9SChen Yu 	old->rapl_pkg_perf_status = new->rapl_pkg_perf_status - old->rapl_pkg_perf_status;
149387e15da9SChen Yu 	old->rapl_dram_perf_status = new->rapl_dram_perf_status - old->rapl_dram_perf_status;
1494ba3dec99SLen Brown 
1495388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1496388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1497388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1498388e9c81SLen Brown 		else
1499388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1500388e9c81SLen Brown 	}
1501388e9c81SLen Brown 
1502ba3dec99SLen Brown 	return 0;
1503103a8feaSLen Brown }
1504103a8feaSLen Brown 
15051b439f01SLen Brown void delta_core(struct core_data *new, struct core_data *old)
1506c98d5d94SLen Brown {
1507388e9c81SLen Brown 	int i;
1508388e9c81SLen Brown 	struct msr_counter *mp;
1509388e9c81SLen Brown 
1510c98d5d94SLen Brown 	old->c3 = new->c3 - old->c3;
1511c98d5d94SLen Brown 	old->c6 = new->c6 - old->c6;
1512c98d5d94SLen Brown 	old->c7 = new->c7 - old->c7;
1513889facbeSLen Brown 	old->core_temp_c = new->core_temp_c;
1514eae97e05SChen Yu 	old->core_throt_cnt = new->core_throt_cnt;
15150539ba11SLen Brown 	old->mc6_us = new->mc6_us - old->mc6_us;
1516388e9c81SLen Brown 
15179392bd98SCalvin Walton 	DELTA_WRAP32(new->core_energy, old->core_energy);
15189392bd98SCalvin Walton 
1519388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1520388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1521388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1522388e9c81SLen Brown 		else
1523388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1524388e9c81SLen Brown 	}
1525103a8feaSLen Brown }
1526c98d5d94SLen Brown 
15271e9042b9SSrinivas Pandruvada int soft_c1_residency_display(int bic)
15281e9042b9SSrinivas Pandruvada {
15291e9042b9SSrinivas Pandruvada 	if (!DO_BIC(BIC_CPU_c1) || use_c1_residency_msr)
15301e9042b9SSrinivas Pandruvada 		return 0;
15311e9042b9SSrinivas Pandruvada 
15321e9042b9SSrinivas Pandruvada 	return DO_BIC_READ(bic);
15331e9042b9SSrinivas Pandruvada }
15341e9042b9SSrinivas Pandruvada 
1535c3ae331dSLen Brown /*
1536c3ae331dSLen Brown  * old = new - old
1537c3ae331dSLen Brown  */
15381b439f01SLen Brown int delta_thread(struct thread_data *new, struct thread_data *old, struct core_data *core_delta)
1539c98d5d94SLen Brown {
1540388e9c81SLen Brown 	int i;
1541388e9c81SLen Brown 	struct msr_counter *mp;
1542388e9c81SLen Brown 
15434c2122d4SLen Brown 	/* we run cpuid just the 1st time, copy the results */
15444c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
15454c2122d4SLen Brown 		new->apic_id = old->apic_id;
15464c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
15474c2122d4SLen Brown 		new->x2apic_id = old->x2apic_id;
15484c2122d4SLen Brown 
15493f44a5c6SLen Brown 	/*
15503f44a5c6SLen Brown 	 * the timestamps from start of measurement interval are in "old"
15513f44a5c6SLen Brown 	 * the timestamp from end of measurement interval are in "new"
15523f44a5c6SLen Brown 	 * over-write old w/ new so we can print end of interval values
15533f44a5c6SLen Brown 	 */
15543f44a5c6SLen Brown 
1555d4794f25SYazen Ghannam 	timersub(&new->tv_begin, &old->tv_begin, &old->tv_delta);
15563f44a5c6SLen Brown 	old->tv_begin = new->tv_begin;
15573f44a5c6SLen Brown 	old->tv_end = new->tv_end;
15583f44a5c6SLen Brown 
1559c98d5d94SLen Brown 	old->tsc = new->tsc - old->tsc;
1560c98d5d94SLen Brown 
1561103a8feaSLen Brown 	/* check for TSC < 1 Mcycles over interval */
1562b2c95d90SJosh Triplett 	if (old->tsc < (1000 * 1000))
1563b2c95d90SJosh Triplett 		errx(-3, "Insanely slow TSC rate, TSC stops in idle?\n"
1564b2c95d90SJosh Triplett 		     "You can disable all c-states by booting with \"idle=poll\"\n"
1565b2c95d90SJosh Triplett 		     "or just the deep ones with \"processor.max_cstate=1\"");
1566103a8feaSLen Brown 
1567c98d5d94SLen Brown 	old->c1 = new->c1 - old->c1;
1568c98d5d94SLen Brown 
15691b439f01SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
1570c98d5d94SLen Brown 		if ((new->aperf > old->aperf) && (new->mperf > old->mperf)) {
1571c98d5d94SLen Brown 			old->aperf = new->aperf - old->aperf;
1572c98d5d94SLen Brown 			old->mperf = new->mperf - old->mperf;
1573c98d5d94SLen Brown 		} else {
1574ba3dec99SLen Brown 			return -1;
1575103a8feaSLen Brown 		}
1576a729617cSLen Brown 	}
1577103a8feaSLen Brown 
1578144b44b1SLen Brown 	if (use_c1_residency_msr) {
1579144b44b1SLen Brown 		/*
1580144b44b1SLen Brown 		 * Some models have a dedicated C1 residency MSR,
1581144b44b1SLen Brown 		 * which should be more accurate than the derivation below.
1582144b44b1SLen Brown 		 */
1583144b44b1SLen Brown 	} else {
1584103a8feaSLen Brown 		/*
1585c3ae331dSLen Brown 		 * As counter collection is not atomic,
1586c3ae331dSLen Brown 		 * it is possible for mperf's non-halted cycles + idle states
1587103a8feaSLen Brown 		 * to exceed TSC's all cycles: show c1 = 0% in that case.
1588103a8feaSLen Brown 		 */
158995149369SLen Brown 		if ((old->mperf + core_delta->c3 + core_delta->c6 + core_delta->c7) > (old->tsc * tsc_tweak))
1590c98d5d94SLen Brown 			old->c1 = 0;
1591c98d5d94SLen Brown 		else {
1592c98d5d94SLen Brown 			/* normal case, derive c1 */
1593008d396eSLen Brown 			old->c1 = (old->tsc * tsc_tweak) - old->mperf - core_delta->c3
1594c98d5d94SLen Brown 			    - core_delta->c6 - core_delta->c7;
1595c98d5d94SLen Brown 		}
1596144b44b1SLen Brown 	}
1597c3ae331dSLen Brown 
1598c98d5d94SLen Brown 	if (old->mperf == 0) {
1599b7d8c148SLen Brown 		if (debug > 1)
1600b7d8c148SLen Brown 			fprintf(outf, "cpu%d MPERF 0!\n", old->cpu_id);
1601c98d5d94SLen Brown 		old->mperf = 1;	/* divide by 0 protection */
1602c98d5d94SLen Brown 	}
1603103a8feaSLen Brown 
16042af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
16052af4f9b8SLen Brown 		old->instr_count = new->instr_count - old->instr_count;
16062af4f9b8SLen Brown 
1607812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
1608562a2d37SLen Brown 		old->irq_count = new->irq_count - old->irq_count;
1609562a2d37SLen Brown 
1610812db3f7SLen Brown 	if (DO_BIC(BIC_SMI))
16111ed51011SLen Brown 		old->smi_count = new->smi_count - old->smi_count;
1612ba3dec99SLen Brown 
1613388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1614388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1615388e9c81SLen Brown 			old->counter[i] = new->counter[i];
1616388e9c81SLen Brown 		else
1617388e9c81SLen Brown 			old->counter[i] = new->counter[i] - old->counter[i];
1618388e9c81SLen Brown 	}
1619ba3dec99SLen Brown 	return 0;
1620103a8feaSLen Brown }
1621c98d5d94SLen Brown 
1622c98d5d94SLen Brown int delta_cpu(struct thread_data *t, struct core_data *c,
16231b439f01SLen Brown 	      struct pkg_data *p, struct thread_data *t2, struct core_data *c2, struct pkg_data *p2)
1624c98d5d94SLen Brown {
1625ba3dec99SLen Brown 	int retval = 0;
1626ba3dec99SLen Brown 
1627c98d5d94SLen Brown 	/* calculate core delta only for 1st thread in core */
1628c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_THREAD_IN_CORE)
1629c98d5d94SLen Brown 		delta_core(c, c2);
1630c98d5d94SLen Brown 
1631c98d5d94SLen Brown 	/* always calculate thread delta */
1632ba3dec99SLen Brown 	retval = delta_thread(t, t2, c2);	/* c2 is core delta */
1633ba3dec99SLen Brown 	if (retval)
1634ba3dec99SLen Brown 		return retval;
1635c98d5d94SLen Brown 
1636c98d5d94SLen Brown 	/* calculate package delta only for 1st core in package */
1637c98d5d94SLen Brown 	if (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)
1638ba3dec99SLen Brown 		retval = delta_package(p, p2);
1639c98d5d94SLen Brown 
1640ba3dec99SLen Brown 	return retval;
1641103a8feaSLen Brown }
1642103a8feaSLen Brown 
1643c98d5d94SLen Brown void clear_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1644103a8feaSLen Brown {
1645388e9c81SLen Brown 	int i;
1646388e9c81SLen Brown 	struct msr_counter *mp;
1647388e9c81SLen Brown 
16483f44a5c6SLen Brown 	t->tv_begin.tv_sec = 0;
16493f44a5c6SLen Brown 	t->tv_begin.tv_usec = 0;
16503f44a5c6SLen Brown 	t->tv_end.tv_sec = 0;
16513f44a5c6SLen Brown 	t->tv_end.tv_usec = 0;
1652d4794f25SYazen Ghannam 	t->tv_delta.tv_sec = 0;
1653d4794f25SYazen Ghannam 	t->tv_delta.tv_usec = 0;
16543f44a5c6SLen Brown 
1655c98d5d94SLen Brown 	t->tsc = 0;
1656c98d5d94SLen Brown 	t->aperf = 0;
1657c98d5d94SLen Brown 	t->mperf = 0;
1658c98d5d94SLen Brown 	t->c1 = 0;
1659103a8feaSLen Brown 
16602af4f9b8SLen Brown 	t->instr_count = 0;
16612af4f9b8SLen Brown 
1662562a2d37SLen Brown 	t->irq_count = 0;
1663562a2d37SLen Brown 	t->smi_count = 0;
1664562a2d37SLen Brown 
1665c98d5d94SLen Brown 	/* tells format_counters to dump all fields from this set */
1666c98d5d94SLen Brown 	t->flags = CPU_IS_FIRST_THREAD_IN_CORE | CPU_IS_FIRST_CORE_IN_PACKAGE;
1667c98d5d94SLen Brown 
1668c98d5d94SLen Brown 	c->c3 = 0;
1669c98d5d94SLen Brown 	c->c6 = 0;
1670c98d5d94SLen Brown 	c->c7 = 0;
16710539ba11SLen Brown 	c->mc6_us = 0;
1672889facbeSLen Brown 	c->core_temp_c = 0;
16739392bd98SCalvin Walton 	c->core_energy = 0;
1674eae97e05SChen Yu 	c->core_throt_cnt = 0;
1675c98d5d94SLen Brown 
16760b2bb692SLen Brown 	p->pkg_wtd_core_c0 = 0;
16770b2bb692SLen Brown 	p->pkg_any_core_c0 = 0;
16780b2bb692SLen Brown 	p->pkg_any_gfxe_c0 = 0;
16790b2bb692SLen Brown 	p->pkg_both_core_gfxe_c0 = 0;
16800b2bb692SLen Brown 
1681c98d5d94SLen Brown 	p->pc2 = 0;
16820f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1683c98d5d94SLen Brown 		p->pc3 = 0;
16840f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1685c98d5d94SLen Brown 		p->pc6 = 0;
16860f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1687c98d5d94SLen Brown 		p->pc7 = 0;
1688ca58710fSKristen Carlson Accardi 	p->pc8 = 0;
1689ca58710fSKristen Carlson Accardi 	p->pc9 = 0;
1690ca58710fSKristen Carlson Accardi 	p->pc10 = 0;
1691be0e54c4SLen Brown 	p->cpu_lpi = 0;
1692be0e54c4SLen Brown 	p->sys_lpi = 0;
1693889facbeSLen Brown 
1694889facbeSLen Brown 	p->energy_pkg = 0;
1695889facbeSLen Brown 	p->energy_dram = 0;
1696889facbeSLen Brown 	p->energy_cores = 0;
1697889facbeSLen Brown 	p->energy_gfx = 0;
1698889facbeSLen Brown 	p->rapl_pkg_perf_status = 0;
1699889facbeSLen Brown 	p->rapl_dram_perf_status = 0;
1700889facbeSLen Brown 	p->pkg_temp_c = 0;
170127d47356SLen Brown 
1702fdf676e5SLen Brown 	p->gfx_rc6_ms = 0;
1703a5c6d65dSLen Brown 	p->uncore_mhz = 0;
170427d47356SLen Brown 	p->gfx_mhz = 0;
1705b4b91569SRafael Antognolli 	p->gfx_act_mhz = 0;
1706388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next)
1707388e9c81SLen Brown 		t->counter[i] = 0;
1708388e9c81SLen Brown 
1709388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next)
1710388e9c81SLen Brown 		c->counter[i] = 0;
1711388e9c81SLen Brown 
1712388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next)
1713388e9c81SLen Brown 		p->counter[i] = 0;
1714103a8feaSLen Brown }
17151b439f01SLen Brown 
17161b439f01SLen Brown int sum_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1717103a8feaSLen Brown {
1718388e9c81SLen Brown 	int i;
1719388e9c81SLen Brown 	struct msr_counter *mp;
1720388e9c81SLen Brown 
17214c2122d4SLen Brown 	/* copy un-changing apic_id's */
17224c2122d4SLen Brown 	if (DO_BIC(BIC_APIC))
17234c2122d4SLen Brown 		average.threads.apic_id = t->apic_id;
17244c2122d4SLen Brown 	if (DO_BIC(BIC_X2APIC))
17254c2122d4SLen Brown 		average.threads.x2apic_id = t->x2apic_id;
17264c2122d4SLen Brown 
17273f44a5c6SLen Brown 	/* remember first tv_begin */
17283f44a5c6SLen Brown 	if (average.threads.tv_begin.tv_sec == 0)
17293f44a5c6SLen Brown 		average.threads.tv_begin = t->tv_begin;
17303f44a5c6SLen Brown 
17313f44a5c6SLen Brown 	/* remember last tv_end */
17323f44a5c6SLen Brown 	average.threads.tv_end = t->tv_end;
17333f44a5c6SLen Brown 
1734c98d5d94SLen Brown 	average.threads.tsc += t->tsc;
1735c98d5d94SLen Brown 	average.threads.aperf += t->aperf;
1736c98d5d94SLen Brown 	average.threads.mperf += t->mperf;
1737c98d5d94SLen Brown 	average.threads.c1 += t->c1;
173815aaa346SLen Brown 
17392af4f9b8SLen Brown 	average.threads.instr_count += t->instr_count;
17402af4f9b8SLen Brown 
1741562a2d37SLen Brown 	average.threads.irq_count += t->irq_count;
1742562a2d37SLen Brown 	average.threads.smi_count += t->smi_count;
1743562a2d37SLen Brown 
1744388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1745388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1746388e9c81SLen Brown 			continue;
1747388e9c81SLen Brown 		average.threads.counter[i] += t->counter[i];
1748388e9c81SLen Brown 	}
1749388e9c81SLen Brown 
1750c98d5d94SLen Brown 	/* sum per-core values only for 1st thread in core */
1751c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
1752c98d5d94SLen Brown 		return 0;
1753c98d5d94SLen Brown 
1754c98d5d94SLen Brown 	average.cores.c3 += c->c3;
1755c98d5d94SLen Brown 	average.cores.c6 += c->c6;
1756c98d5d94SLen Brown 	average.cores.c7 += c->c7;
17570539ba11SLen Brown 	average.cores.mc6_us += c->mc6_us;
1758c98d5d94SLen Brown 
1759889facbeSLen Brown 	average.cores.core_temp_c = MAX(average.cores.core_temp_c, c->core_temp_c);
1760eae97e05SChen Yu 	average.cores.core_throt_cnt = MAX(average.cores.core_throt_cnt, c->core_throt_cnt);
1761889facbeSLen Brown 
17629392bd98SCalvin Walton 	average.cores.core_energy += c->core_energy;
17639392bd98SCalvin Walton 
1764388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1765388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1766388e9c81SLen Brown 			continue;
1767388e9c81SLen Brown 		average.cores.counter[i] += c->counter[i];
1768388e9c81SLen Brown 	}
1769388e9c81SLen Brown 
1770c98d5d94SLen Brown 	/* sum per-pkg values only for 1st core in pkg */
1771c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
1772c98d5d94SLen Brown 		return 0;
1773c98d5d94SLen Brown 
1774a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
17750b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 += p->pkg_wtd_core_c0;
1776a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
17770b2bb692SLen Brown 		average.packages.pkg_any_core_c0 += p->pkg_any_core_c0;
1778a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
17790b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 += p->pkg_any_gfxe_c0;
1780a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
17810b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 += p->pkg_both_core_gfxe_c0;
17820b2bb692SLen Brown 
1783c98d5d94SLen Brown 	average.packages.pc2 += p->pc2;
17840f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1785c98d5d94SLen Brown 		average.packages.pc3 += p->pc3;
17860f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1787c98d5d94SLen Brown 		average.packages.pc6 += p->pc6;
17880f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1789c98d5d94SLen Brown 		average.packages.pc7 += p->pc7;
1790ca58710fSKristen Carlson Accardi 	average.packages.pc8 += p->pc8;
1791ca58710fSKristen Carlson Accardi 	average.packages.pc9 += p->pc9;
1792ca58710fSKristen Carlson Accardi 	average.packages.pc10 += p->pc10;
1793c98d5d94SLen Brown 
1794be0e54c4SLen Brown 	average.packages.cpu_lpi = p->cpu_lpi;
1795be0e54c4SLen Brown 	average.packages.sys_lpi = p->sys_lpi;
1796be0e54c4SLen Brown 
1797889facbeSLen Brown 	average.packages.energy_pkg += p->energy_pkg;
1798889facbeSLen Brown 	average.packages.energy_dram += p->energy_dram;
1799889facbeSLen Brown 	average.packages.energy_cores += p->energy_cores;
1800889facbeSLen Brown 	average.packages.energy_gfx += p->energy_gfx;
1801889facbeSLen Brown 
1802fdf676e5SLen Brown 	average.packages.gfx_rc6_ms = p->gfx_rc6_ms;
1803a5c6d65dSLen Brown 	average.packages.uncore_mhz = p->uncore_mhz;
180427d47356SLen Brown 	average.packages.gfx_mhz = p->gfx_mhz;
1805b4b91569SRafael Antognolli 	average.packages.gfx_act_mhz = p->gfx_act_mhz;
180627d47356SLen Brown 
1807889facbeSLen Brown 	average.packages.pkg_temp_c = MAX(average.packages.pkg_temp_c, p->pkg_temp_c);
1808889facbeSLen Brown 
1809889facbeSLen Brown 	average.packages.rapl_pkg_perf_status += p->rapl_pkg_perf_status;
1810889facbeSLen Brown 	average.packages.rapl_dram_perf_status += p->rapl_dram_perf_status;
1811388e9c81SLen Brown 
1812388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1813388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1814388e9c81SLen Brown 			continue;
1815388e9c81SLen Brown 		average.packages.counter[i] += p->counter[i];
1816388e9c81SLen Brown 	}
1817c98d5d94SLen Brown 	return 0;
1818c98d5d94SLen Brown }
18191b439f01SLen Brown 
1820c98d5d94SLen Brown /*
1821c98d5d94SLen Brown  * sum the counters for all cpus in the system
1822c98d5d94SLen Brown  * compute the weighted average
1823c98d5d94SLen Brown  */
18241b439f01SLen Brown void compute_average(struct thread_data *t, struct core_data *c, struct pkg_data *p)
1825c98d5d94SLen Brown {
1826388e9c81SLen Brown 	int i;
1827388e9c81SLen Brown 	struct msr_counter *mp;
1828388e9c81SLen Brown 
1829c98d5d94SLen Brown 	clear_counters(&average.threads, &average.cores, &average.packages);
1830c98d5d94SLen Brown 
1831c98d5d94SLen Brown 	for_all_cpus(sum_counters, t, c, p);
1832c98d5d94SLen Brown 
1833d4794f25SYazen Ghannam 	/* Use the global time delta for the average. */
1834d4794f25SYazen Ghannam 	average.threads.tv_delta = tv_delta;
1835d4794f25SYazen Ghannam 
1836c98d5d94SLen Brown 	average.threads.tsc /= topo.num_cpus;
1837c98d5d94SLen Brown 	average.threads.aperf /= topo.num_cpus;
1838c98d5d94SLen Brown 	average.threads.mperf /= topo.num_cpus;
18392af4f9b8SLen Brown 	average.threads.instr_count /= topo.num_cpus;
1840c98d5d94SLen Brown 	average.threads.c1 /= topo.num_cpus;
1841c98d5d94SLen Brown 
18420de6c0dfSLen Brown 	if (average.threads.irq_count > 9999999)
18430de6c0dfSLen Brown 		sums_need_wide_columns = 1;
18440de6c0dfSLen Brown 
1845c98d5d94SLen Brown 	average.cores.c3 /= topo.num_cores;
1846c98d5d94SLen Brown 	average.cores.c6 /= topo.num_cores;
1847c98d5d94SLen Brown 	average.cores.c7 /= topo.num_cores;
18480539ba11SLen Brown 	average.cores.mc6_us /= topo.num_cores;
1849c98d5d94SLen Brown 
1850a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0))
18510b2bb692SLen Brown 		average.packages.pkg_wtd_core_c0 /= topo.num_packages;
1852a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0))
18530b2bb692SLen Brown 		average.packages.pkg_any_core_c0 /= topo.num_packages;
1854a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0))
18550b2bb692SLen Brown 		average.packages.pkg_any_gfxe_c0 /= topo.num_packages;
1856a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX))
18570b2bb692SLen Brown 		average.packages.pkg_both_core_gfxe_c0 /= topo.num_packages;
18580b2bb692SLen Brown 
1859c98d5d94SLen Brown 	average.packages.pc2 /= topo.num_packages;
18600f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
1861c98d5d94SLen Brown 		average.packages.pc3 /= topo.num_packages;
18620f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6))
1863c98d5d94SLen Brown 		average.packages.pc6 /= topo.num_packages;
18640f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
1865c98d5d94SLen Brown 		average.packages.pc7 /= topo.num_packages;
1866ca58710fSKristen Carlson Accardi 
1867ca58710fSKristen Carlson Accardi 	average.packages.pc8 /= topo.num_packages;
1868ca58710fSKristen Carlson Accardi 	average.packages.pc9 /= topo.num_packages;
1869ca58710fSKristen Carlson Accardi 	average.packages.pc10 /= topo.num_packages;
1870388e9c81SLen Brown 
1871388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
1872388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1873388e9c81SLen Brown 			continue;
18740de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18750de6c0dfSLen Brown 			if (average.threads.counter[i] > 9999999)
18760de6c0dfSLen Brown 				sums_need_wide_columns = 1;
187741618e63SLen Brown 			continue;
18780de6c0dfSLen Brown 		}
1879388e9c81SLen Brown 		average.threads.counter[i] /= topo.num_cpus;
1880388e9c81SLen Brown 	}
1881388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
1882388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1883388e9c81SLen Brown 			continue;
18840de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18850de6c0dfSLen Brown 			if (average.cores.counter[i] > 9999999)
18860de6c0dfSLen Brown 				sums_need_wide_columns = 1;
18870de6c0dfSLen Brown 		}
1888388e9c81SLen Brown 		average.cores.counter[i] /= topo.num_cores;
1889388e9c81SLen Brown 	}
1890388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
1891388e9c81SLen Brown 		if (mp->format == FORMAT_RAW)
1892388e9c81SLen Brown 			continue;
18930de6c0dfSLen Brown 		if (mp->type == COUNTER_ITEMS) {
18940de6c0dfSLen Brown 			if (average.packages.counter[i] > 9999999)
18950de6c0dfSLen Brown 				sums_need_wide_columns = 1;
18960de6c0dfSLen Brown 		}
1897388e9c81SLen Brown 		average.packages.counter[i] /= topo.num_packages;
1898388e9c81SLen Brown 	}
1899c98d5d94SLen Brown }
1900c98d5d94SLen Brown 
1901c98d5d94SLen Brown static unsigned long long rdtsc(void)
1902c98d5d94SLen Brown {
1903c98d5d94SLen Brown 	unsigned int low, high;
1904c98d5d94SLen Brown 
1905c98d5d94SLen Brown 	asm volatile ("rdtsc":"=a" (low), "=d"(high));
1906c98d5d94SLen Brown 
1907c98d5d94SLen Brown 	return low | ((unsigned long long)high) << 32;
1908c98d5d94SLen Brown }
1909c98d5d94SLen Brown 
1910c98d5d94SLen Brown /*
1911495c7654SLen Brown  * Open a file, and exit on failure
1912495c7654SLen Brown  */
1913495c7654SLen Brown FILE *fopen_or_die(const char *path, const char *mode)
1914495c7654SLen Brown {
1915495c7654SLen Brown 	FILE *filep = fopen(path, mode);
1916495c7654SLen Brown 
1917495c7654SLen Brown 	if (!filep)
1918495c7654SLen Brown 		err(1, "%s: open failed", path);
1919495c7654SLen Brown 	return filep;
1920495c7654SLen Brown }
19211b439f01SLen Brown 
1922495c7654SLen Brown /*
1923495c7654SLen Brown  * snapshot_sysfs_counter()
1924495c7654SLen Brown  *
1925495c7654SLen Brown  * return snapshot of given counter
1926495c7654SLen Brown  */
1927495c7654SLen Brown unsigned long long snapshot_sysfs_counter(char *path)
1928495c7654SLen Brown {
1929495c7654SLen Brown 	FILE *fp;
1930495c7654SLen Brown 	int retval;
1931495c7654SLen Brown 	unsigned long long counter;
1932495c7654SLen Brown 
1933495c7654SLen Brown 	fp = fopen_or_die(path, "r");
1934495c7654SLen Brown 
1935495c7654SLen Brown 	retval = fscanf(fp, "%lld", &counter);
1936495c7654SLen Brown 	if (retval != 1)
1937495c7654SLen Brown 		err(1, "snapshot_sysfs_counter(%s)", path);
1938495c7654SLen Brown 
1939495c7654SLen Brown 	fclose(fp);
1940495c7654SLen Brown 
1941495c7654SLen Brown 	return counter;
1942495c7654SLen Brown }
1943495c7654SLen Brown 
1944495c7654SLen Brown int get_mp(int cpu, struct msr_counter *mp, unsigned long long *counterp)
1945495c7654SLen Brown {
1946495c7654SLen Brown 	if (mp->msr_num != 0) {
1947495c7654SLen Brown 		if (get_msr(cpu, mp->msr_num, counterp))
1948495c7654SLen Brown 			return -1;
1949495c7654SLen Brown 	} else {
195046c27978SLen Brown 		char path[128 + PATH_BYTES];
195141618e63SLen Brown 
195241618e63SLen Brown 		if (mp->flags & SYSFS_PERCPU) {
19531b439f01SLen Brown 			sprintf(path, "/sys/devices/system/cpu/cpu%d/%s", cpu, mp->path);
195441618e63SLen Brown 
195541618e63SLen Brown 			*counterp = snapshot_sysfs_counter(path);
195641618e63SLen Brown 		} else {
1957495c7654SLen Brown 			*counterp = snapshot_sysfs_counter(mp->path);
1958495c7654SLen Brown 		}
195941618e63SLen Brown 	}
1960495c7654SLen Brown 
1961495c7654SLen Brown 	return 0;
1962495c7654SLen Brown }
1963495c7654SLen Brown 
1964a5c6d65dSLen Brown unsigned long long get_uncore_mhz(int package, int die)
1965a5c6d65dSLen Brown {
1966a5c6d65dSLen Brown 	char path[128];
1967a5c6d65dSLen Brown 
1968a5c6d65dSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/current_freq_khz", package,
1969a5c6d65dSLen Brown 		die);
1970a5c6d65dSLen Brown 
1971a5c6d65dSLen Brown 	return (snapshot_sysfs_counter(path) / 1000);
1972a5c6d65dSLen Brown }
1973a5c6d65dSLen Brown 
19746d6501d9SBorislav Petkov int get_epb(int cpu)
19756d6501d9SBorislav Petkov {
19766d6501d9SBorislav Petkov 	char path[128 + PATH_BYTES];
19777f1b11baSBorislav Petkov 	unsigned long long msr;
19786d6501d9SBorislav Petkov 	int ret, epb = -1;
19796d6501d9SBorislav Petkov 	FILE *fp;
19806d6501d9SBorislav Petkov 
19816d6501d9SBorislav Petkov 	sprintf(path, "/sys/devices/system/cpu/cpu%d/power/energy_perf_bias", cpu);
19826d6501d9SBorislav Petkov 
19837f1b11baSBorislav Petkov 	fp = fopen(path, "r");
19847f1b11baSBorislav Petkov 	if (!fp)
19857f1b11baSBorislav Petkov 		goto msr_fallback;
19866d6501d9SBorislav Petkov 
19876d6501d9SBorislav Petkov 	ret = fscanf(fp, "%d", &epb);
19886d6501d9SBorislav Petkov 	if (ret != 1)
19896d6501d9SBorislav Petkov 		err(1, "%s(%s)", __func__, path);
19906d6501d9SBorislav Petkov 
19916d6501d9SBorislav Petkov 	fclose(fp);
19926d6501d9SBorislav Petkov 
19936d6501d9SBorislav Petkov 	return epb;
19947f1b11baSBorislav Petkov 
19957f1b11baSBorislav Petkov msr_fallback:
19967f1b11baSBorislav Petkov 	get_msr(cpu, MSR_IA32_ENERGY_PERF_BIAS, &msr);
19977f1b11baSBorislav Petkov 
19987f1b11baSBorislav Petkov 	return msr & 0xf;
19996d6501d9SBorislav Petkov }
20006d6501d9SBorislav Petkov 
20014c2122d4SLen Brown void get_apic_id(struct thread_data *t)
20024c2122d4SLen Brown {
200334041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
200434041551SLen Brown 
200534041551SLen Brown 	if (DO_BIC(BIC_APIC)) {
200634041551SLen Brown 		eax = ebx = ecx = edx = 0;
200734041551SLen Brown 		__cpuid(1, eax, ebx, ecx, edx);
200834041551SLen Brown 
200934041551SLen Brown 		t->apic_id = (ebx >> 24) & 0xff;
201034041551SLen Brown 	}
201134041551SLen Brown 
201234041551SLen Brown 	if (!DO_BIC(BIC_X2APIC))
201334041551SLen Brown 		return;
201434041551SLen Brown 
2015c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine) {
201634041551SLen Brown 		unsigned int topology_extensions;
201734041551SLen Brown 
201834041551SLen Brown 		if (max_extended_level < 0x8000001e)
201934041551SLen Brown 			return;
20204c2122d4SLen Brown 
20214c2122d4SLen Brown 		eax = ebx = ecx = edx = 0;
202234041551SLen Brown 		__cpuid(0x80000001, eax, ebx, ecx, edx);
202334041551SLen Brown 		topology_extensions = ecx & (1 << 22);
202434041551SLen Brown 
202534041551SLen Brown 		if (topology_extensions == 0)
202634041551SLen Brown 			return;
202734041551SLen Brown 
202834041551SLen Brown 		eax = ebx = ecx = edx = 0;
202934041551SLen Brown 		__cpuid(0x8000001e, eax, ebx, ecx, edx);
203034041551SLen Brown 
203134041551SLen Brown 		t->x2apic_id = eax;
203234041551SLen Brown 		return;
203334041551SLen Brown 	}
20344c2122d4SLen Brown 
20354c2122d4SLen Brown 	if (!genuine_intel)
20364c2122d4SLen Brown 		return;
20374c2122d4SLen Brown 
20384c2122d4SLen Brown 	if (max_level < 0xb)
20394c2122d4SLen Brown 		return;
20404c2122d4SLen Brown 
20414c2122d4SLen Brown 	ecx = 0;
20424c2122d4SLen Brown 	__cpuid(0xb, eax, ebx, ecx, edx);
20434c2122d4SLen Brown 	t->x2apic_id = edx;
20444c2122d4SLen Brown 
204534041551SLen Brown 	if (debug && (t->apic_id != (t->x2apic_id & 0xff)))
20461b439f01SLen Brown 		fprintf(outf, "cpu%d: BIOS BUG: apic 0x%x x2apic 0x%x\n", t->cpu_id, t->apic_id, t->x2apic_id);
20474c2122d4SLen Brown }
20484c2122d4SLen Brown 
2049eae97e05SChen Yu int get_core_throt_cnt(int cpu, unsigned long long *cnt)
2050eae97e05SChen Yu {
2051eae97e05SChen Yu 	char path[128 + PATH_BYTES];
2052eae97e05SChen Yu 	unsigned long long tmp;
2053eae97e05SChen Yu 	FILE *fp;
2054eae97e05SChen Yu 	int ret;
2055eae97e05SChen Yu 
2056eae97e05SChen Yu 	sprintf(path, "/sys/devices/system/cpu/cpu%d/thermal_throttle/core_throttle_count", cpu);
2057eae97e05SChen Yu 	fp = fopen(path, "r");
2058eae97e05SChen Yu 	if (!fp)
2059eae97e05SChen Yu 		return -1;
2060eae97e05SChen Yu 	ret = fscanf(fp, "%lld", &tmp);
20615e5fd36cSColin Ian King 	fclose(fp);
2062eae97e05SChen Yu 	if (ret != 1)
2063eae97e05SChen Yu 		return -1;
2064eae97e05SChen Yu 	*cnt = tmp;
2065eae97e05SChen Yu 
2066eae97e05SChen Yu 	return 0;
2067eae97e05SChen Yu }
2068eae97e05SChen Yu 
2069495c7654SLen Brown /*
2070c98d5d94SLen Brown  * get_counters(...)
2071c98d5d94SLen Brown  * migrate to cpu
2072c98d5d94SLen Brown  * acquire and record local counters for that cpu
2073c98d5d94SLen Brown  */
2074c98d5d94SLen Brown int get_counters(struct thread_data *t, struct core_data *c, struct pkg_data *p)
2075c98d5d94SLen Brown {
2076c98d5d94SLen Brown 	int cpu = t->cpu_id;
2077889facbeSLen Brown 	unsigned long long msr;
20780102b067SLen Brown 	int aperf_mperf_retry_count = 0;
2079388e9c81SLen Brown 	struct msr_counter *mp;
2080388e9c81SLen Brown 	int i;
2081c98d5d94SLen Brown 
2082e52966c0SLen Brown 	if (cpu_migrate(cpu)) {
20833d7772eaSLen Brown 		fprintf(outf, "get_counters: Could not migrate to CPU %d\n", cpu);
208415aaa346SLen Brown 		return -1;
2085e52966c0SLen Brown 	}
208615aaa346SLen Brown 
2087d4794f25SYazen Ghannam 	gettimeofday(&t->tv_begin, (struct timezone *)NULL);
2088d4794f25SYazen Ghannam 
20894c2122d4SLen Brown 	if (first_counter_read)
20904c2122d4SLen Brown 		get_apic_id(t);
20910102b067SLen Brown retry:
2092c98d5d94SLen Brown 	t->tsc = rdtsc();	/* we are running on local CPU of interest */
209315aaa346SLen Brown 
20941b439f01SLen Brown 	if (DO_BIC(BIC_Avg_MHz) || DO_BIC(BIC_Busy) || DO_BIC(BIC_Bzy_MHz) || soft_c1_residency_display(BIC_Avg_MHz)) {
20950102b067SLen Brown 		unsigned long long tsc_before, tsc_between, tsc_after, aperf_time, mperf_time;
20960102b067SLen Brown 
20970102b067SLen Brown 		/*
20980102b067SLen Brown 		 * The TSC, APERF and MPERF must be read together for
20990102b067SLen Brown 		 * APERF/MPERF and MPERF/TSC to give accurate results.
21000102b067SLen Brown 		 *
21010102b067SLen Brown 		 * Unfortunately, APERF and MPERF are read by
21020102b067SLen Brown 		 * individual system call, so delays may occur
21030102b067SLen Brown 		 * between them.  If the time to read them
21040102b067SLen Brown 		 * varies by a large amount, we re-read them.
21050102b067SLen Brown 		 */
21060102b067SLen Brown 
21070102b067SLen Brown 		/*
21080102b067SLen Brown 		 * This initial dummy APERF read has been seen to
21090102b067SLen Brown 		 * reduce jitter in the subsequent reads.
21100102b067SLen Brown 		 */
21110102b067SLen Brown 
21129c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
2113c98d5d94SLen Brown 			return -3;
21140102b067SLen Brown 
21150102b067SLen Brown 		t->tsc = rdtsc();	/* re-read close to APERF */
21160102b067SLen Brown 
21170102b067SLen Brown 		tsc_before = t->tsc;
21180102b067SLen Brown 
21190102b067SLen Brown 		if (get_msr(cpu, MSR_IA32_APERF, &t->aperf))
21200102b067SLen Brown 			return -3;
21210102b067SLen Brown 
21220102b067SLen Brown 		tsc_between = rdtsc();
21230102b067SLen Brown 
21249c63a650SLen Brown 		if (get_msr(cpu, MSR_IA32_MPERF, &t->mperf))
2125c98d5d94SLen Brown 			return -4;
21260102b067SLen Brown 
21270102b067SLen Brown 		tsc_after = rdtsc();
21280102b067SLen Brown 
21290102b067SLen Brown 		aperf_time = tsc_between - tsc_before;
21300102b067SLen Brown 		mperf_time = tsc_after - tsc_between;
21310102b067SLen Brown 
21320102b067SLen Brown 		/*
21330102b067SLen Brown 		 * If the system call latency to read APERF and MPERF
21340102b067SLen Brown 		 * differ by more than 2x, then try again.
21350102b067SLen Brown 		 */
21360102b067SLen Brown 		if ((aperf_time > (2 * mperf_time)) || (mperf_time > (2 * aperf_time))) {
21370102b067SLen Brown 			aperf_mperf_retry_count++;
21380102b067SLen Brown 			if (aperf_mperf_retry_count < 5)
21390102b067SLen Brown 				goto retry;
21400102b067SLen Brown 			else
21411b439f01SLen Brown 				warnx("cpu%d jitter %lld %lld", cpu, aperf_time, mperf_time);
21420102b067SLen Brown 		}
21430102b067SLen Brown 		aperf_mperf_retry_count = 0;
21440102b067SLen Brown 
2145b2b34dfeSHubert Chrzaniuk 		t->aperf = t->aperf * aperf_mperf_multiplier;
2146b2b34dfeSHubert Chrzaniuk 		t->mperf = t->mperf * aperf_mperf_multiplier;
214788c3281fSLen Brown 	}
214888c3281fSLen Brown 
21492af4f9b8SLen Brown 	if (DO_BIC(BIC_IPC))
21502af4f9b8SLen Brown 		if (read(get_instr_count_fd(cpu), &t->instr_count, sizeof(long long)) != sizeof(long long))
21512af4f9b8SLen Brown 			return -4;
21522af4f9b8SLen Brown 
2153812db3f7SLen Brown 	if (DO_BIC(BIC_IRQ))
2154562a2d37SLen Brown 		t->irq_count = irqs_per_cpu[cpu];
2155812db3f7SLen Brown 	if (DO_BIC(BIC_SMI)) {
21561ed51011SLen Brown 		if (get_msr(cpu, MSR_SMI_COUNT, &msr))
21571ed51011SLen Brown 			return -5;
21581ed51011SLen Brown 		t->smi_count = msr & 0xFFFFFFFF;
21591ed51011SLen Brown 	}
21600539ba11SLen Brown 	if (DO_BIC(BIC_CPU_c1) && use_c1_residency_msr) {
2161144b44b1SLen Brown 		if (get_msr(cpu, MSR_CORE_C1_RES, &t->c1))
2162144b44b1SLen Brown 			return -6;
2163144b44b1SLen Brown 	}
2164144b44b1SLen Brown 
2165388e9c81SLen Brown 	for (i = 0, mp = sys.tp; mp; i++, mp = mp->next) {
2166495c7654SLen Brown 		if (get_mp(cpu, mp, &t->counter[i]))
2167388e9c81SLen Brown 			return -10;
2168388e9c81SLen Brown 	}
2169388e9c81SLen Brown 
2170c98d5d94SLen Brown 	/* collect core counters only for 1st thread in core */
2171c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
2172f4fdf2b4SLen Brown 		goto done;
2173c98d5d94SLen Brown 
21741e9042b9SSrinivas Pandruvada 	if (DO_BIC(BIC_CPU_c3) || soft_c1_residency_display(BIC_CPU_c3)) {
2175c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C3_RESIDENCY, &c->c3))
2176c98d5d94SLen Brown 			return -6;
2177144b44b1SLen Brown 	}
2178144b44b1SLen Brown 
21791e9042b9SSrinivas Pandruvada 	if ((DO_BIC(BIC_CPU_c6) || soft_c1_residency_display(BIC_CPU_c6)) && !do_knl_cstates) {
2180c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C6_RESIDENCY, &c->c6))
2181c98d5d94SLen Brown 			return -7;
21821e9042b9SSrinivas Pandruvada 	} else if (do_knl_cstates || soft_c1_residency_display(BIC_CPU_c6)) {
2183fb5d4327SDasaratharaman Chandramouli 		if (get_msr(cpu, MSR_KNL_CORE_C6_RESIDENCY, &c->c6))
2184fb5d4327SDasaratharaman Chandramouli 			return -7;
2185103a8feaSLen Brown 	}
218615aaa346SLen Brown 
21877ab5ff49SZhang Rui 	if (DO_BIC(BIC_CPU_c7) || soft_c1_residency_display(BIC_CPU_c7)) {
2188c98d5d94SLen Brown 		if (get_msr(cpu, MSR_CORE_C7_RESIDENCY, &c->c7))
2189c98d5d94SLen Brown 			return -8;
21907ab5ff49SZhang Rui 		else if (t->is_atom) {
21917ab5ff49SZhang Rui 			/*
21927ab5ff49SZhang Rui 			 * For Atom CPUs that has core cstate deeper than c6,
21937ab5ff49SZhang Rui 			 * MSR_CORE_C6_RESIDENCY returns residency of cc6 and deeper.
21947ab5ff49SZhang Rui 			 * Minus CC7 (and deeper cstates) residency to get
21957ab5ff49SZhang Rui 			 * accturate cc6 residency.
21967ab5ff49SZhang Rui 			 */
21977ab5ff49SZhang Rui 			c->c6 -= c->c7;
21987ab5ff49SZhang Rui 		}
21997ab5ff49SZhang Rui 	}
2200c98d5d94SLen Brown 
22010539ba11SLen Brown 	if (DO_BIC(BIC_Mod_c6))
22020539ba11SLen Brown 		if (get_msr(cpu, MSR_MODULE_C6_RES_MS, &c->mc6_us))
22030539ba11SLen Brown 			return -8;
22040539ba11SLen Brown 
2205812db3f7SLen Brown 	if (DO_BIC(BIC_CoreTmp)) {
2206889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
2207889facbeSLen Brown 			return -9;
220855279aefSZhang Rui 		c->core_temp_c = tj_max - ((msr >> 16) & 0x7F);
2209889facbeSLen Brown 	}
2210889facbeSLen Brown 
2211eae97e05SChen Yu 	if (DO_BIC(BIC_CORE_THROT_CNT))
2212eae97e05SChen Yu 		get_core_throt_cnt(cpu, &c->core_throt_cnt);
2213eae97e05SChen Yu 
22149392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
22159392bd98SCalvin Walton 		if (get_msr(cpu, MSR_CORE_ENERGY_STAT, &msr))
22169392bd98SCalvin Walton 			return -14;
22179392bd98SCalvin Walton 		c->core_energy = msr & 0xFFFFFFFF;
22189392bd98SCalvin Walton 	}
22199392bd98SCalvin Walton 
2220388e9c81SLen Brown 	for (i = 0, mp = sys.cp; mp; i++, mp = mp->next) {
2221495c7654SLen Brown 		if (get_mp(cpu, mp, &c->counter[i]))
2222388e9c81SLen Brown 			return -10;
2223388e9c81SLen Brown 	}
2224889facbeSLen Brown 
2225c98d5d94SLen Brown 	/* collect package counters only for 1st core in package */
2226c98d5d94SLen Brown 	if (!(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
2227f4fdf2b4SLen Brown 		goto done;
222815aaa346SLen Brown 
2229a99d8730SLen Brown 	if (DO_BIC(BIC_Totl_c0)) {
22300b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_WEIGHTED_CORE_C0_RES, &p->pkg_wtd_core_c0))
22310b2bb692SLen Brown 			return -10;
2232a99d8730SLen Brown 	}
2233a99d8730SLen Brown 	if (DO_BIC(BIC_Any_c0)) {
22340b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_CORE_C0_RES, &p->pkg_any_core_c0))
22350b2bb692SLen Brown 			return -11;
2236a99d8730SLen Brown 	}
2237a99d8730SLen Brown 	if (DO_BIC(BIC_GFX_c0)) {
22380b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_ANY_GFXE_C0_RES, &p->pkg_any_gfxe_c0))
22390b2bb692SLen Brown 			return -12;
2240a99d8730SLen Brown 	}
2241a99d8730SLen Brown 	if (DO_BIC(BIC_CPUGFX)) {
22420b2bb692SLen Brown 		if (get_msr(cpu, MSR_PKG_BOTH_CORE_GFXE_C0_RES, &p->pkg_both_core_gfxe_c0))
22430b2bb692SLen Brown 			return -13;
22440b2bb692SLen Brown 	}
22450f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc3))
2246c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C3_RESIDENCY, &p->pc3))
2247c98d5d94SLen Brown 			return -9;
22480f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc6)) {
22490539ba11SLen Brown 		if (do_slm_cstates) {
22500539ba11SLen Brown 			if (get_msr(cpu, MSR_ATOM_PKG_C6_RESIDENCY, &p->pc6))
22510539ba11SLen Brown 				return -10;
22520539ba11SLen Brown 		} else {
2253c98d5d94SLen Brown 			if (get_msr(cpu, MSR_PKG_C6_RESIDENCY, &p->pc6))
2254c98d5d94SLen Brown 				return -10;
22550539ba11SLen Brown 		}
22560539ba11SLen Brown 	}
22570539ba11SLen Brown 
22580f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc2))
2259c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C2_RESIDENCY, &p->pc2))
2260c98d5d94SLen Brown 			return -11;
22610f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc7))
2262c98d5d94SLen Brown 		if (get_msr(cpu, MSR_PKG_C7_RESIDENCY, &p->pc7))
2263c98d5d94SLen Brown 			return -12;
22640f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc8))
2265ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C8_RESIDENCY, &p->pc8))
2266ca58710fSKristen Carlson Accardi 			return -13;
22670f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc9))
2268ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C9_RESIDENCY, &p->pc9))
2269ca58710fSKristen Carlson Accardi 			return -13;
22700f47c08dSLen Brown 	if (DO_BIC(BIC_Pkgpc10))
2271ca58710fSKristen Carlson Accardi 		if (get_msr(cpu, MSR_PKG_C10_RESIDENCY, &p->pc10))
2272ca58710fSKristen Carlson Accardi 			return -13;
22730f47c08dSLen Brown 
2274be0e54c4SLen Brown 	if (DO_BIC(BIC_CPU_LPI))
2275be0e54c4SLen Brown 		p->cpu_lpi = cpuidle_cur_cpu_lpi_us;
2276be0e54c4SLen Brown 	if (DO_BIC(BIC_SYS_LPI))
2277be0e54c4SLen Brown 		p->sys_lpi = cpuidle_cur_sys_lpi_us;
2278be0e54c4SLen Brown 
2279889facbeSLen Brown 	if (do_rapl & RAPL_PKG) {
22809972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_ENERGY_STATUS, &msr))
2281889facbeSLen Brown 			return -13;
22829972d5d8SChen Yu 		p->energy_pkg = msr;
2283889facbeSLen Brown 	}
22849148494cSJacob Pan 	if (do_rapl & RAPL_CORES_ENERGY_STATUS) {
22859972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PP0_ENERGY_STATUS, &msr))
2286889facbeSLen Brown 			return -14;
22879972d5d8SChen Yu 		p->energy_cores = msr;
2288889facbeSLen Brown 	}
2289889facbeSLen Brown 	if (do_rapl & RAPL_DRAM) {
22909972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_DRAM_ENERGY_STATUS, &msr))
2291889facbeSLen Brown 			return -15;
22929972d5d8SChen Yu 		p->energy_dram = msr;
2293889facbeSLen Brown 	}
2294889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
22959972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PP1_ENERGY_STATUS, &msr))
2296889facbeSLen Brown 			return -16;
22979972d5d8SChen Yu 		p->energy_gfx = msr;
2298889facbeSLen Brown 	}
2299889facbeSLen Brown 	if (do_rapl & RAPL_PKG_PERF_STATUS) {
23009972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_PERF_STATUS, &msr))
2301889facbeSLen Brown 			return -16;
23029972d5d8SChen Yu 		p->rapl_pkg_perf_status = msr;
2303889facbeSLen Brown 	}
2304889facbeSLen Brown 	if (do_rapl & RAPL_DRAM_PERF_STATUS) {
23059972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_DRAM_PERF_STATUS, &msr))
2306889facbeSLen Brown 			return -16;
23079972d5d8SChen Yu 		p->rapl_dram_perf_status = msr;
2308889facbeSLen Brown 	}
23093316f99aSCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
23109972d5d8SChen Yu 		if (get_msr_sum(cpu, MSR_PKG_ENERGY_STAT, &msr))
23113316f99aSCalvin Walton 			return -13;
23129972d5d8SChen Yu 		p->energy_pkg = msr;
23133316f99aSCalvin Walton 	}
2314812db3f7SLen Brown 	if (DO_BIC(BIC_PkgTmp)) {
2315889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
2316889facbeSLen Brown 			return -17;
231755279aefSZhang Rui 		p->pkg_temp_c = tj_max - ((msr >> 16) & 0x7F);
2318889facbeSLen Brown 	}
2319fdf676e5SLen Brown 
2320812db3f7SLen Brown 	if (DO_BIC(BIC_GFX_rc6))
2321fdf676e5SLen Brown 		p->gfx_rc6_ms = gfx_cur_rc6_ms;
2322fdf676e5SLen Brown 
2323a5c6d65dSLen Brown 	/* n.b. assume die0 uncore frequency applies to whole package */
2324a5c6d65dSLen Brown 	if (DO_BIC(BIC_UNCORE_MHZ))
2325a5c6d65dSLen Brown 		p->uncore_mhz = get_uncore_mhz(p->package_id, 0);
2326a5c6d65dSLen Brown 
2327812db3f7SLen Brown 	if (DO_BIC(BIC_GFXMHz))
232827d47356SLen Brown 		p->gfx_mhz = gfx_cur_mhz;
232927d47356SLen Brown 
2330b4b91569SRafael Antognolli 	if (DO_BIC(BIC_GFXACTMHz))
2331b4b91569SRafael Antognolli 		p->gfx_act_mhz = gfx_act_mhz;
2332b4b91569SRafael Antognolli 
2333388e9c81SLen Brown 	for (i = 0, mp = sys.pp; mp; i++, mp = mp->next) {
2334495c7654SLen Brown 		if (get_mp(cpu, mp, &p->counter[i]))
2335388e9c81SLen Brown 			return -10;
2336388e9c81SLen Brown 	}
2337f4fdf2b4SLen Brown done:
2338f4fdf2b4SLen Brown 	gettimeofday(&t->tv_end, (struct timezone *)NULL);
2339388e9c81SLen Brown 
234015aaa346SLen Brown 	return 0;
2341103a8feaSLen Brown }
2342103a8feaSLen Brown 
2343ee7e38e3SLen Brown /*
2344ee7e38e3SLen Brown  * MSR_PKG_CST_CONFIG_CONTROL decoding for pkg_cstate_limit:
2345ee7e38e3SLen Brown  * If you change the values, note they are used both in comparisons
2346ee7e38e3SLen Brown  * (>= PCL__7) and to index pkg_cstate_limit_strings[].
2347ee7e38e3SLen Brown  */
2348ee7e38e3SLen Brown 
2349ee7e38e3SLen Brown #define PCLUKN 0		/* Unknown */
2350ee7e38e3SLen Brown #define PCLRSV 1		/* Reserved */
2351ee7e38e3SLen Brown #define PCL__0 2		/* PC0 */
2352ee7e38e3SLen Brown #define PCL__1 3		/* PC1 */
2353ee7e38e3SLen Brown #define PCL__2 4		/* PC2 */
2354ee7e38e3SLen Brown #define PCL__3 5		/* PC3 */
2355ee7e38e3SLen Brown #define PCL__4 6		/* PC4 */
2356ee7e38e3SLen Brown #define PCL__6 7		/* PC6 */
2357ee7e38e3SLen Brown #define PCL_6N 8		/* PC6 No Retention */
2358ee7e38e3SLen Brown #define PCL_6R 9		/* PC6 Retention */
2359ee7e38e3SLen Brown #define PCL__7 10		/* PC7 */
2360ee7e38e3SLen Brown #define PCL_7S 11		/* PC7 Shrink */
23610b2bb692SLen Brown #define PCL__8 12		/* PC8 */
23620b2bb692SLen Brown #define PCL__9 13		/* PC9 */
2363445640a5SLen Brown #define PCL_10 14		/* PC10 */
2364445640a5SLen Brown #define PCLUNL 15		/* Unlimited */
2365ee7e38e3SLen Brown 
2366ee7e38e3SLen Brown int pkg_cstate_limit = PCLUKN;
2367ee7e38e3SLen Brown char *pkg_cstate_limit_strings[] = { "reserved", "unknown", "pc0", "pc1", "pc2",
23681b439f01SLen Brown 	"pc3", "pc4", "pc6", "pc6n", "pc6r", "pc7", "pc7s", "pc8", "pc9", "pc10", "unlimited"
23691b439f01SLen Brown };
2370ee7e38e3SLen Brown 
23711b439f01SLen Brown int nhm_pkg_cstate_limits[16] =
23721b439f01SLen Brown     { PCL__0, PCL__1, PCL__3, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
237338c6663aSLen Brown 	PCLRSV, PCLRSV
237438c6663aSLen Brown };
237538c6663aSLen Brown 
23761b439f01SLen Brown int snb_pkg_cstate_limits[16] =
23771b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCL__7, PCL_7S, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
237838c6663aSLen Brown 	PCLRSV, PCLRSV
237938c6663aSLen Brown };
238038c6663aSLen Brown 
23811b439f01SLen Brown int hsw_pkg_cstate_limits[16] =
23821b439f01SLen Brown     { PCL__0, PCL__2, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
238338c6663aSLen Brown 	PCLRSV, PCLRSV
238438c6663aSLen Brown };
238538c6663aSLen Brown 
23861b439f01SLen Brown int slv_pkg_cstate_limits[16] =
23871b439f01SLen Brown     { PCL__0, PCL__1, PCLRSV, PCLRSV, PCL__4, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
238838c6663aSLen Brown 	PCL__6, PCL__7
238938c6663aSLen Brown };
239038c6663aSLen Brown 
23911b439f01SLen Brown int amt_pkg_cstate_limits[16] =
23921b439f01SLen Brown     { PCLUNL, PCL__1, PCL__2, PCLRSV, PCLRSV, PCLRSV, PCL__6, PCL__7, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
239338c6663aSLen Brown 	PCLRSV, PCLRSV
239438c6663aSLen Brown };
239538c6663aSLen Brown 
23961b439f01SLen Brown int phi_pkg_cstate_limits[16] =
23971b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
239838c6663aSLen Brown 	PCLRSV, PCLRSV
239938c6663aSLen Brown };
240038c6663aSLen Brown 
24011b439f01SLen Brown int glm_pkg_cstate_limits[16] =
24021b439f01SLen Brown     { PCLUNL, PCL__1, PCL__3, PCL__6, PCL__7, PCL_7S, PCL__8, PCL__9, PCL_10, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
240338c6663aSLen Brown 	PCLRSV, PCLRSV
240438c6663aSLen Brown };
240538c6663aSLen Brown 
24061b439f01SLen Brown int skx_pkg_cstate_limits[16] =
24071b439f01SLen Brown     { PCL__0, PCL__2, PCL_6N, PCL_6R, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
240838c6663aSLen Brown 	PCLRSV, PCLRSV
240938c6663aSLen Brown };
241038c6663aSLen Brown 
24111b439f01SLen Brown int icx_pkg_cstate_limits[16] =
2412a1b6f487SArtem Bityutskiy     { PCL__0, PCL__2, PCL__6, PCL__6, PCLRSV, PCLRSV, PCLRSV, PCLUNL, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV, PCLRSV,
241338c6663aSLen Brown 	PCLRSV, PCLRSV
241438c6663aSLen Brown };
2415a2b7b749SLen Brown 
24161b439f01SLen Brown static void calculate_tsc_tweak()
2417a2b7b749SLen Brown {
2418a2b7b749SLen Brown 	tsc_tweak = base_hz / tsc_hz;
2419a2b7b749SLen Brown }
2420a2b7b749SLen Brown 
2421aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model);
2422aeb01e6dSChen Yu 
24231b439f01SLen Brown static void dump_nhm_platform_info(void)
2424103a8feaSLen Brown {
2425103a8feaSLen Brown 	unsigned long long msr;
2426103a8feaSLen Brown 	unsigned int ratio;
2427103a8feaSLen Brown 
2428ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
2429103a8feaSLen Brown 
2430b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_PLATFORM_INFO: 0x%08llx\n", base_cpu, msr);
24316574a5d5SLen Brown 
2432103a8feaSLen Brown 	ratio = (msr >> 40) & 0xFF;
24331b439f01SLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz max efficiency frequency\n", ratio, bclk, ratio * bclk);
2434103a8feaSLen Brown 
2435103a8feaSLen Brown 	ratio = (msr >> 8) & 0xFF;
24361b439f01SLen Brown 	fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
2437103a8feaSLen Brown 
24387ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_IA32_POWER_CTL, &msr);
2439b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_IA32_POWER_CTL: 0x%08llx (C1E auto-promotion: %sabled)\n",
2440bfae2052SLen Brown 		base_cpu, msr, msr & 0x2 ? "EN" : "DIS");
244167920418SLen Brown 
2442aeb01e6dSChen Yu 	/* C-state Pre-wake Disable (CSTATE_PREWAKE_DISABLE) */
2443aeb01e6dSChen Yu 	if (dis_cstate_prewake)
24441b439f01SLen Brown 		fprintf(outf, "C-state Pre-wake: %sabled\n", msr & 0x40000000 ? "DIS" : "EN");
2445aeb01e6dSChen Yu 
2446fcd17211SLen Brown 	return;
2447fcd17211SLen Brown }
2448fcd17211SLen Brown 
24491b439f01SLen Brown static void dump_hsw_turbo_ratio_limits(void)
2450fcd17211SLen Brown {
2451fcd17211SLen Brown 	unsigned long long msr;
2452fcd17211SLen Brown 	unsigned int ratio;
2453fcd17211SLen Brown 
24547ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT2, &msr);
2455fcd17211SLen Brown 
2456b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT2: 0x%08llx\n", base_cpu, msr);
2457fcd17211SLen Brown 
2458fcd17211SLen Brown 	ratio = (msr >> 8) & 0xFF;
2459fcd17211SLen Brown 	if (ratio)
24601b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 18 active cores\n", ratio, bclk, ratio * bclk);
2461fcd17211SLen Brown 
2462fcd17211SLen Brown 	ratio = (msr >> 0) & 0xFF;
2463fcd17211SLen Brown 	if (ratio)
24641b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 17 active cores\n", ratio, bclk, ratio * bclk);
2465fcd17211SLen Brown 	return;
2466fcd17211SLen Brown }
2467fcd17211SLen Brown 
24681b439f01SLen Brown static void dump_ivt_turbo_ratio_limits(void)
2469fcd17211SLen Brown {
2470fcd17211SLen Brown 	unsigned long long msr;
2471fcd17211SLen Brown 	unsigned int ratio;
24726574a5d5SLen Brown 
24737ce7d5deSPrarit Bhargava 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &msr);
24746574a5d5SLen Brown 
2475b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, msr);
24766574a5d5SLen Brown 
24776574a5d5SLen Brown 	ratio = (msr >> 56) & 0xFF;
24786574a5d5SLen Brown 	if (ratio)
24791b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 16 active cores\n", ratio, bclk, ratio * bclk);
24806574a5d5SLen Brown 
24816574a5d5SLen Brown 	ratio = (msr >> 48) & 0xFF;
24826574a5d5SLen Brown 	if (ratio)
24831b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 15 active cores\n", ratio, bclk, ratio * bclk);
24846574a5d5SLen Brown 
24856574a5d5SLen Brown 	ratio = (msr >> 40) & 0xFF;
24866574a5d5SLen Brown 	if (ratio)
24871b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 14 active cores\n", ratio, bclk, ratio * bclk);
24886574a5d5SLen Brown 
24896574a5d5SLen Brown 	ratio = (msr >> 32) & 0xFF;
24906574a5d5SLen Brown 	if (ratio)
24911b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 13 active cores\n", ratio, bclk, ratio * bclk);
24926574a5d5SLen Brown 
24936574a5d5SLen Brown 	ratio = (msr >> 24) & 0xFF;
24946574a5d5SLen Brown 	if (ratio)
24951b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 12 active cores\n", ratio, bclk, ratio * bclk);
24966574a5d5SLen Brown 
24976574a5d5SLen Brown 	ratio = (msr >> 16) & 0xFF;
24986574a5d5SLen Brown 	if (ratio)
24991b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 11 active cores\n", ratio, bclk, ratio * bclk);
25006574a5d5SLen Brown 
25016574a5d5SLen Brown 	ratio = (msr >> 8) & 0xFF;
25026574a5d5SLen Brown 	if (ratio)
25031b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 10 active cores\n", ratio, bclk, ratio * bclk);
25046574a5d5SLen Brown 
25056574a5d5SLen Brown 	ratio = (msr >> 0) & 0xFF;
25066574a5d5SLen Brown 	if (ratio)
25071b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 9 active cores\n", ratio, bclk, ratio * bclk);
2508103a8feaSLen Brown 	return;
2509fcd17211SLen Brown }
25101b439f01SLen Brown 
251131e07522SLen Brown int has_turbo_ratio_group_limits(int family, int model)
251231e07522SLen Brown {
251331e07522SLen Brown 
251431e07522SLen Brown 	if (!genuine_intel)
251531e07522SLen Brown 		return 0;
251631e07522SLen Brown 
25179878bf7aSLen Brown 	if (family != 6)
25189878bf7aSLen Brown 		return 0;
25199878bf7aSLen Brown 
252031e07522SLen Brown 	switch (model) {
252131e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
252231e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
252325368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
2524684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:
25255ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
252620de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:
252731e07522SLen Brown 		return 1;
25289878bf7aSLen Brown 	default:
252931e07522SLen Brown 		return 0;
253031e07522SLen Brown 	}
25319878bf7aSLen Brown }
2532fcd17211SLen Brown 
25334af184eeSLen Brown static void dump_turbo_ratio_limits(int trl_msr_offset, int family, int model)
2534fcd17211SLen Brown {
253531e07522SLen Brown 	unsigned long long msr, core_counts;
25365d622845SLen Brown 	int shift;
2537103a8feaSLen Brown 
25384af184eeSLen Brown 	get_msr(base_cpu, trl_msr_offset, &msr);
25394af184eeSLen Brown 	fprintf(outf, "cpu%d: MSR_%sTURBO_RATIO_LIMIT: 0x%08llx\n",
25404af184eeSLen Brown 		base_cpu, trl_msr_offset == MSR_SECONDARY_TURBO_RATIO_LIMIT ? "SECONDARY" : "", msr);
25416574a5d5SLen Brown 
254231e07522SLen Brown 	if (has_turbo_ratio_group_limits(family, model)) {
254331e07522SLen Brown 		get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT1, &core_counts);
254431e07522SLen Brown 		fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT1: 0x%08llx\n", base_cpu, core_counts);
254531e07522SLen Brown 	} else {
254631e07522SLen Brown 		core_counts = 0x0807060504030201;
254731e07522SLen Brown 	}
254831e07522SLen Brown 
25495d622845SLen Brown 	for (shift = 56; shift >= 0; shift -= 8) {
25505d622845SLen Brown 		unsigned int ratio, group_size;
25516574a5d5SLen Brown 
25525d622845SLen Brown 		ratio = (msr >> shift) & 0xFF;
25535d622845SLen Brown 		group_size = (core_counts >> shift) & 0xFF;
25546574a5d5SLen Brown 		if (ratio)
255531e07522SLen Brown 			fprintf(outf, "%d * %.1f = %.1f MHz max turbo %d active cores\n",
255631e07522SLen Brown 				ratio, bclk, ratio * bclk, group_size);
25575d622845SLen Brown 	}
25586574a5d5SLen Brown 
2559fcd17211SLen Brown 	return;
2560fcd17211SLen Brown }
25613a9a941dSLen Brown 
25621b439f01SLen Brown static void dump_atom_turbo_ratio_limits(void)
25630f7887c4SLen Brown {
25640f7887c4SLen Brown 	unsigned long long msr;
25650f7887c4SLen Brown 	unsigned int ratio;
25660f7887c4SLen Brown 
25670f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_RATIOS, &msr);
25680f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25690f7887c4SLen Brown 
25700f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
25710f7887c4SLen Brown 	if (ratio)
25721b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz minimum operating frequency\n", ratio, bclk, ratio * bclk);
25730f7887c4SLen Brown 
25740f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
25750f7887c4SLen Brown 	if (ratio)
25761b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz low frequency mode (LFM)\n", ratio, bclk, ratio * bclk);
25770f7887c4SLen Brown 
25780f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
25790f7887c4SLen Brown 	if (ratio)
25801b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz base frequency\n", ratio, bclk, ratio * bclk);
25810f7887c4SLen Brown 
25820f7887c4SLen Brown 	get_msr(base_cpu, MSR_ATOM_CORE_TURBO_RATIOS, &msr);
25830f7887c4SLen Brown 	fprintf(outf, "cpu%d: MSR_ATOM_CORE_TURBO_RATIOS: 0x%08llx\n", base_cpu, msr & 0xFFFFFFFF);
25840f7887c4SLen Brown 
25850f7887c4SLen Brown 	ratio = (msr >> 24) & 0x3F;
25860f7887c4SLen Brown 	if (ratio)
25871b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 4 active cores\n", ratio, bclk, ratio * bclk);
25880f7887c4SLen Brown 
25890f7887c4SLen Brown 	ratio = (msr >> 16) & 0x3F;
25900f7887c4SLen Brown 	if (ratio)
25911b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 3 active cores\n", ratio, bclk, ratio * bclk);
25920f7887c4SLen Brown 
25930f7887c4SLen Brown 	ratio = (msr >> 8) & 0x3F;
25940f7887c4SLen Brown 	if (ratio)
25951b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 2 active cores\n", ratio, bclk, ratio * bclk);
25960f7887c4SLen Brown 
25970f7887c4SLen Brown 	ratio = (msr >> 0) & 0x3F;
25980f7887c4SLen Brown 	if (ratio)
25991b439f01SLen Brown 		fprintf(outf, "%d * %.1f = %.1f MHz max turbo 1 active core\n", ratio, bclk, ratio * bclk);
26000f7887c4SLen Brown }
26010f7887c4SLen Brown 
26021b439f01SLen Brown static void dump_knl_turbo_ratio_limits(void)
2603fb5d4327SDasaratharaman Chandramouli {
2604cbf97abaSHubert Chrzaniuk 	const unsigned int buckets_no = 7;
2605cbf97abaSHubert Chrzaniuk 
2606fb5d4327SDasaratharaman Chandramouli 	unsigned long long msr;
2607cbf97abaSHubert Chrzaniuk 	int delta_cores, delta_ratio;
2608cbf97abaSHubert Chrzaniuk 	int i, b_nr;
2609cbf97abaSHubert Chrzaniuk 	unsigned int cores[buckets_no];
2610cbf97abaSHubert Chrzaniuk 	unsigned int ratio[buckets_no];
2611fb5d4327SDasaratharaman Chandramouli 
2612ebf5926aSSrinivas Pandruvada 	get_msr(base_cpu, MSR_TURBO_RATIO_LIMIT, &msr);
2613fb5d4327SDasaratharaman Chandramouli 
26141b439f01SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_RATIO_LIMIT: 0x%08llx\n", base_cpu, msr);
2615fb5d4327SDasaratharaman Chandramouli 
26161e3ec5cdSRandy Dunlap 	/*
2617fb5d4327SDasaratharaman Chandramouli 	 * Turbo encoding in KNL is as follows:
2618cbf97abaSHubert Chrzaniuk 	 * [0] -- Reserved
2619cbf97abaSHubert Chrzaniuk 	 * [7:1] -- Base value of number of active cores of bucket 1.
2620fb5d4327SDasaratharaman Chandramouli 	 * [15:8] -- Base value of freq ratio of bucket 1.
2621fb5d4327SDasaratharaman Chandramouli 	 * [20:16] -- +ve delta of number of active cores of bucket 2.
2622fb5d4327SDasaratharaman Chandramouli 	 * i.e. active cores of bucket 2 =
2623fb5d4327SDasaratharaman Chandramouli 	 * active cores of bucket 1 + delta
2624fb5d4327SDasaratharaman Chandramouli 	 * [23:21] -- Negative delta of freq ratio of bucket 2.
2625fb5d4327SDasaratharaman Chandramouli 	 * i.e. freq ratio of bucket 2 =
2626fb5d4327SDasaratharaman Chandramouli 	 * freq ratio of bucket 1 - delta
2627fb5d4327SDasaratharaman Chandramouli 	 * [28:24]-- +ve delta of number of active cores of bucket 3.
2628fb5d4327SDasaratharaman Chandramouli 	 * [31:29]-- -ve delta of freq ratio of bucket 3.
2629fb5d4327SDasaratharaman Chandramouli 	 * [36:32]-- +ve delta of number of active cores of bucket 4.
2630fb5d4327SDasaratharaman Chandramouli 	 * [39:37]-- -ve delta of freq ratio of bucket 4.
2631fb5d4327SDasaratharaman Chandramouli 	 * [44:40]-- +ve delta of number of active cores of bucket 5.
2632fb5d4327SDasaratharaman Chandramouli 	 * [47:45]-- -ve delta of freq ratio of bucket 5.
2633fb5d4327SDasaratharaman Chandramouli 	 * [52:48]-- +ve delta of number of active cores of bucket 6.
2634fb5d4327SDasaratharaman Chandramouli 	 * [55:53]-- -ve delta of freq ratio of bucket 6.
2635fb5d4327SDasaratharaman Chandramouli 	 * [60:56]-- +ve delta of number of active cores of bucket 7.
2636fb5d4327SDasaratharaman Chandramouli 	 * [63:61]-- -ve delta of freq ratio of bucket 7.
2637fb5d4327SDasaratharaman Chandramouli 	 */
2638fb5d4327SDasaratharaman Chandramouli 
2639cbf97abaSHubert Chrzaniuk 	b_nr = 0;
2640cbf97abaSHubert Chrzaniuk 	cores[b_nr] = (msr & 0xFF) >> 1;
2641cbf97abaSHubert Chrzaniuk 	ratio[b_nr] = (msr >> 8) & 0xFF;
2642cbf97abaSHubert Chrzaniuk 
2643cbf97abaSHubert Chrzaniuk 	for (i = 16; i < 64; i += 8) {
2644fb5d4327SDasaratharaman Chandramouli 		delta_cores = (msr >> i) & 0x1F;
2645cbf97abaSHubert Chrzaniuk 		delta_ratio = (msr >> (i + 5)) & 0x7;
2646fb5d4327SDasaratharaman Chandramouli 
2647cbf97abaSHubert Chrzaniuk 		cores[b_nr + 1] = cores[b_nr] + delta_cores;
2648cbf97abaSHubert Chrzaniuk 		ratio[b_nr + 1] = ratio[b_nr] - delta_ratio;
2649cbf97abaSHubert Chrzaniuk 		b_nr++;
2650fb5d4327SDasaratharaman Chandramouli 	}
2651cbf97abaSHubert Chrzaniuk 
2652cbf97abaSHubert Chrzaniuk 	for (i = buckets_no - 1; i >= 0; i--)
2653cbf97abaSHubert Chrzaniuk 		if (i > 0 ? ratio[i] != ratio[i - 1] : 1)
2654b7d8c148SLen Brown 			fprintf(outf,
2655710f273bSLen Brown 				"%d * %.1f = %.1f MHz max turbo %d active cores\n",
2656cbf97abaSHubert Chrzaniuk 				ratio[i], bclk, ratio[i] * bclk, cores[i]);
2657fb5d4327SDasaratharaman Chandramouli }
2658fb5d4327SDasaratharaman Chandramouli 
26591b439f01SLen Brown static void dump_nhm_cst_cfg(void)
2660fcd17211SLen Brown {
2661fcd17211SLen Brown 	unsigned long long msr;
2662fcd17211SLen Brown 
26631df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
2664fcd17211SLen Brown 
26651df2e55aSLen Brown 	fprintf(outf, "cpu%d: MSR_PKG_CST_CONFIG_CONTROL: 0x%08llx", base_cpu, msr);
2666fcd17211SLen Brown 
26673e8b62bfSArtem Bityutskiy 	fprintf(outf, " (%s%s%s%s%slocked, pkg-cstate-limit=%d (%s)",
2668fcd17211SLen Brown 		(msr & SNB_C3_AUTO_UNDEMOTE) ? "UNdemote-C3, " : "",
2669fcd17211SLen Brown 		(msr & SNB_C1_AUTO_UNDEMOTE) ? "UNdemote-C1, " : "",
2670fcd17211SLen Brown 		(msr & NHM_C3_AUTO_DEMOTE) ? "demote-C3, " : "",
2671fcd17211SLen Brown 		(msr & NHM_C1_AUTO_DEMOTE) ? "demote-C1, " : "",
26721b439f01SLen Brown 		(msr & (1 << 15)) ? "" : "UN", (unsigned int)msr & 0xF, pkg_cstate_limit_strings[pkg_cstate_limit]);
2673ac980e13SArtem Bityutskiy 
2674ac980e13SArtem Bityutskiy #define AUTOMATIC_CSTATE_CONVERSION		(1UL << 16)
2675ac980e13SArtem Bityutskiy 	if (has_automatic_cstate_conversion) {
26761b439f01SLen Brown 		fprintf(outf, ", automatic c-state conversion=%s", (msr & AUTOMATIC_CSTATE_CONVERSION) ? "on" : "off");
2677ac980e13SArtem Bityutskiy 	}
2678ac980e13SArtem Bityutskiy 
2679ac980e13SArtem Bityutskiy 	fprintf(outf, ")\n");
2680ac980e13SArtem Bityutskiy 
2681fcd17211SLen Brown 	return;
2682103a8feaSLen Brown }
2683103a8feaSLen Brown 
26841b439f01SLen Brown static void dump_config_tdp(void)
26856fb3143bSLen Brown {
26866fb3143bSLen Brown 	unsigned long long msr;
26876fb3143bSLen Brown 
26886fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_NOMINAL, &msr);
2689b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_NOMINAL: 0x%08llx", base_cpu, msr);
2690685b535bSChen Yu 	fprintf(outf, " (base_ratio=%d)\n", (unsigned int)msr & 0xFF);
26916fb3143bSLen Brown 
26926fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_1, &msr);
2693b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_1: 0x%08llx (", base_cpu, msr);
26946fb3143bSLen Brown 	if (msr) {
2695685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL1=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2696685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL1=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2697685b535bSChen Yu 		fprintf(outf, "LVL1_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2698685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL1=%d", (unsigned int)(msr) & 0x7FFF);
26996fb3143bSLen Brown 	}
2700b7d8c148SLen Brown 	fprintf(outf, ")\n");
27016fb3143bSLen Brown 
27026fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_LEVEL_2, &msr);
2703b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_LEVEL_2: 0x%08llx (", base_cpu, msr);
27046fb3143bSLen Brown 	if (msr) {
2705685b535bSChen Yu 		fprintf(outf, "PKG_MIN_PWR_LVL2=%d ", (unsigned int)(msr >> 48) & 0x7FFF);
2706685b535bSChen Yu 		fprintf(outf, "PKG_MAX_PWR_LVL2=%d ", (unsigned int)(msr >> 32) & 0x7FFF);
2707685b535bSChen Yu 		fprintf(outf, "LVL2_RATIO=%d ", (unsigned int)(msr >> 16) & 0xFF);
2708685b535bSChen Yu 		fprintf(outf, "PKG_TDP_LVL2=%d", (unsigned int)(msr) & 0x7FFF);
27096fb3143bSLen Brown 	}
2710b7d8c148SLen Brown 	fprintf(outf, ")\n");
27116fb3143bSLen Brown 
27126fb3143bSLen Brown 	get_msr(base_cpu, MSR_CONFIG_TDP_CONTROL, &msr);
2713b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_CONFIG_TDP_CONTROL: 0x%08llx (", base_cpu, msr);
27146fb3143bSLen Brown 	if ((msr) & 0x3)
2715b7d8c148SLen Brown 		fprintf(outf, "TDP_LEVEL=%d ", (unsigned int)(msr) & 0x3);
2716b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2717b7d8c148SLen Brown 	fprintf(outf, ")\n");
27186fb3143bSLen Brown 
27196fb3143bSLen Brown 	get_msr(base_cpu, MSR_TURBO_ACTIVATION_RATIO, &msr);
2720b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_TURBO_ACTIVATION_RATIO: 0x%08llx (", base_cpu, msr);
2721685b535bSChen Yu 	fprintf(outf, "MAX_NON_TURBO_RATIO=%d", (unsigned int)(msr) & 0xFF);
2722b7d8c148SLen Brown 	fprintf(outf, " lock=%d", (unsigned int)(msr >> 31) & 1);
2723b7d8c148SLen Brown 	fprintf(outf, ")\n");
27246fb3143bSLen Brown }
27255a63426eSLen Brown 
27265a63426eSLen Brown unsigned int irtl_time_units[] = { 1, 32, 1024, 32768, 1048576, 33554432, 0, 0 };
27275a63426eSLen Brown 
27285a63426eSLen Brown void print_irtl(void)
27295a63426eSLen Brown {
27305a63426eSLen Brown 	unsigned long long msr;
27315a63426eSLen Brown 
27325a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC3_IRTL, &msr);
27335a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC3_IRTL: 0x%08llx (", base_cpu, msr);
27345a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27355a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27365a63426eSLen Brown 
27375a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC6_IRTL, &msr);
27385a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC6_IRTL: 0x%08llx (", base_cpu, msr);
27395a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27405a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27415a63426eSLen Brown 
27425a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC7_IRTL, &msr);
27435a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC7_IRTL: 0x%08llx (", base_cpu, msr);
27445a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27455a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27465a63426eSLen Brown 
27475a63426eSLen Brown 	if (!do_irtl_hsw)
27485a63426eSLen Brown 		return;
27495a63426eSLen Brown 
27505a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC8_IRTL, &msr);
27515a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC8_IRTL: 0x%08llx (", base_cpu, msr);
27525a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27535a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27545a63426eSLen Brown 
27555a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC9_IRTL, &msr);
27565a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC9_IRTL: 0x%08llx (", base_cpu, msr);
27575a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27585a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27595a63426eSLen Brown 
27605a63426eSLen Brown 	get_msr(base_cpu, MSR_PKGC10_IRTL, &msr);
27615a63426eSLen Brown 	fprintf(outf, "cpu%d: MSR_PKGC10_IRTL: 0x%08llx (", base_cpu, msr);
27625a63426eSLen Brown 	fprintf(outf, "%svalid, %lld ns)\n", msr & (1 << 15) ? "" : "NOT",
27635a63426eSLen Brown 		(msr & 0x3FF) * irtl_time_units[(msr >> 10) & 0x3]);
27645a63426eSLen Brown 
27655a63426eSLen Brown }
27661b439f01SLen Brown 
276736229897SLen Brown void free_fd_percpu(void)
276836229897SLen Brown {
276936229897SLen Brown 	int i;
277036229897SLen Brown 
277101a67adfSMika Westerberg 	for (i = 0; i < topo.max_cpu_num + 1; ++i) {
277236229897SLen Brown 		if (fd_percpu[i] != 0)
277336229897SLen Brown 			close(fd_percpu[i]);
277436229897SLen Brown 	}
277536229897SLen Brown 
277636229897SLen Brown 	free(fd_percpu);
27776fb3143bSLen Brown }
27786fb3143bSLen Brown 
2779c98d5d94SLen Brown void free_all_buffers(void)
2780103a8feaSLen Brown {
27810e2d8f05SLen Brown 	int i;
27820e2d8f05SLen Brown 
2783c98d5d94SLen Brown 	CPU_FREE(cpu_present_set);
2784c98d5d94SLen Brown 	cpu_present_set = NULL;
278536229897SLen Brown 	cpu_present_setsize = 0;
2786103a8feaSLen Brown 
2787c98d5d94SLen Brown 	CPU_FREE(cpu_affinity_set);
2788c98d5d94SLen Brown 	cpu_affinity_set = NULL;
2789c98d5d94SLen Brown 	cpu_affinity_setsize = 0;
2790103a8feaSLen Brown 
2791c98d5d94SLen Brown 	free(thread_even);
2792c98d5d94SLen Brown 	free(core_even);
2793c98d5d94SLen Brown 	free(package_even);
2794103a8feaSLen Brown 
2795c98d5d94SLen Brown 	thread_even = NULL;
2796c98d5d94SLen Brown 	core_even = NULL;
2797c98d5d94SLen Brown 	package_even = NULL;
2798103a8feaSLen Brown 
2799c98d5d94SLen Brown 	free(thread_odd);
2800c98d5d94SLen Brown 	free(core_odd);
2801c98d5d94SLen Brown 	free(package_odd);
2802103a8feaSLen Brown 
2803c98d5d94SLen Brown 	thread_odd = NULL;
2804c98d5d94SLen Brown 	core_odd = NULL;
2805c98d5d94SLen Brown 	package_odd = NULL;
2806103a8feaSLen Brown 
2807c98d5d94SLen Brown 	free(output_buffer);
2808c98d5d94SLen Brown 	output_buffer = NULL;
2809c98d5d94SLen Brown 	outp = NULL;
281036229897SLen Brown 
281136229897SLen Brown 	free_fd_percpu();
2812562a2d37SLen Brown 
2813562a2d37SLen Brown 	free(irq_column_2_cpu);
2814562a2d37SLen Brown 	free(irqs_per_cpu);
28150e2d8f05SLen Brown 
28160e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
28170e2d8f05SLen Brown 		if (cpus[i].put_ids)
28180e2d8f05SLen Brown 			CPU_FREE(cpus[i].put_ids);
28190e2d8f05SLen Brown 	}
28200e2d8f05SLen Brown 	free(cpus);
2821103a8feaSLen Brown }
2822103a8feaSLen Brown 
282357a42a34SJosh Triplett /*
282495aebc44SJosh Triplett  * Parse a file containing a single int.
28256de68fe1SLen Brown  * Return 0 if file can not be opened
28266de68fe1SLen Brown  * Exit if file can be opened, but can not be parsed
282795aebc44SJosh Triplett  */
282895aebc44SJosh Triplett int parse_int_file(const char *fmt, ...)
282995aebc44SJosh Triplett {
283095aebc44SJosh Triplett 	va_list args;
283195aebc44SJosh Triplett 	char path[PATH_MAX];
283295aebc44SJosh Triplett 	FILE *filep;
283395aebc44SJosh Triplett 	int value;
283495aebc44SJosh Triplett 
283595aebc44SJosh Triplett 	va_start(args, fmt);
283695aebc44SJosh Triplett 	vsnprintf(path, sizeof(path), fmt, args);
283795aebc44SJosh Triplett 	va_end(args);
28386de68fe1SLen Brown 	filep = fopen(path, "r");
28396de68fe1SLen Brown 	if (!filep)
28406de68fe1SLen Brown 		return 0;
2841b2c95d90SJosh Triplett 	if (fscanf(filep, "%d", &value) != 1)
2842b2c95d90SJosh Triplett 		err(1, "%s: failed to parse number from file", path);
284395aebc44SJosh Triplett 	fclose(filep);
284495aebc44SJosh Triplett 	return value;
284595aebc44SJosh Triplett }
284695aebc44SJosh Triplett 
284795aebc44SJosh Triplett /*
2848c98d5d94SLen Brown  * cpu_is_first_core_in_package(cpu)
2849c98d5d94SLen Brown  * return 1 if given CPU is 1st core in package
2850c98d5d94SLen Brown  */
2851c98d5d94SLen Brown int cpu_is_first_core_in_package(int cpu)
2852c98d5d94SLen Brown {
285395aebc44SJosh Triplett 	return cpu == parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_siblings_list", cpu);
2854103a8feaSLen Brown }
2855103a8feaSLen Brown 
2856103a8feaSLen Brown int get_physical_package_id(int cpu)
2857103a8feaSLen Brown {
285895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
2859103a8feaSLen Brown }
2860103a8feaSLen Brown 
28616de68fe1SLen Brown int get_die_id(int cpu)
28626de68fe1SLen Brown {
28636de68fe1SLen Brown 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/die_id", cpu);
28646de68fe1SLen Brown }
28656de68fe1SLen Brown 
2866103a8feaSLen Brown int get_core_id(int cpu)
2867103a8feaSLen Brown {
286895aebc44SJosh Triplett 	return parse_int_file("/sys/devices/system/cpu/cpu%d/topology/core_id", cpu);
2869103a8feaSLen Brown }
2870103a8feaSLen Brown 
2871ef605741SPrarit Bhargava void set_node_data(void)
2872ef605741SPrarit Bhargava {
28732ffbb224SPrarit Bhargava 	int pkg, node, lnode, cpu, cpux;
28742ffbb224SPrarit Bhargava 	int cpu_count;
2875ef605741SPrarit Bhargava 
28762ffbb224SPrarit Bhargava 	/* initialize logical_node_id */
28772ffbb224SPrarit Bhargava 	for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu)
28782ffbb224SPrarit Bhargava 		cpus[cpu].logical_node_id = -1;
2879ef605741SPrarit Bhargava 
28802ffbb224SPrarit Bhargava 	cpu_count = 0;
28812ffbb224SPrarit Bhargava 	for (pkg = 0; pkg < topo.num_packages; pkg++) {
28822ffbb224SPrarit Bhargava 		lnode = 0;
28832ffbb224SPrarit Bhargava 		for (cpu = 0; cpu <= topo.max_cpu_num; ++cpu) {
28842ffbb224SPrarit Bhargava 			if (cpus[cpu].physical_package_id != pkg)
2885ef605741SPrarit Bhargava 				continue;
28862ffbb224SPrarit Bhargava 			/* find a cpu with an unset logical_node_id */
28872ffbb224SPrarit Bhargava 			if (cpus[cpu].logical_node_id != -1)
28882ffbb224SPrarit Bhargava 				continue;
28892ffbb224SPrarit Bhargava 			cpus[cpu].logical_node_id = lnode;
2890ef605741SPrarit Bhargava 			node = cpus[cpu].physical_node_id;
28912ffbb224SPrarit Bhargava 			cpu_count++;
28922ffbb224SPrarit Bhargava 			/*
28932ffbb224SPrarit Bhargava 			 * find all matching cpus on this pkg and set
28942ffbb224SPrarit Bhargava 			 * the logical_node_id
28952ffbb224SPrarit Bhargava 			 */
28962ffbb224SPrarit Bhargava 			for (cpux = cpu; cpux <= topo.max_cpu_num; cpux++) {
28971b439f01SLen Brown 				if ((cpus[cpux].physical_package_id == pkg) && (cpus[cpux].physical_node_id == node)) {
28982ffbb224SPrarit Bhargava 					cpus[cpux].logical_node_id = lnode;
28992ffbb224SPrarit Bhargava 					cpu_count++;
2900ef605741SPrarit Bhargava 				}
29012ffbb224SPrarit Bhargava 			}
29022ffbb224SPrarit Bhargava 			lnode++;
29032ffbb224SPrarit Bhargava 			if (lnode > topo.nodes_per_pkg)
29042ffbb224SPrarit Bhargava 				topo.nodes_per_pkg = lnode;
29052ffbb224SPrarit Bhargava 		}
29062ffbb224SPrarit Bhargava 		if (cpu_count >= topo.max_cpu_num)
29072ffbb224SPrarit Bhargava 			break;
29082ffbb224SPrarit Bhargava 	}
2909ef605741SPrarit Bhargava }
2910ef605741SPrarit Bhargava 
2911ef605741SPrarit Bhargava int get_physical_node_id(struct cpu_topology *thiscpu)
2912c98d5d94SLen Brown {
2913c98d5d94SLen Brown 	char path[80];
2914c98d5d94SLen Brown 	FILE *filep;
29150e2d8f05SLen Brown 	int i;
29160e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
2917c98d5d94SLen Brown 
29180e2d8f05SLen Brown 	for (i = 0; i <= topo.max_cpu_num; i++) {
29191b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/node%i/cpulist", cpu, i);
29200e2d8f05SLen Brown 		filep = fopen(path, "r");
29210e2d8f05SLen Brown 		if (!filep)
29220e2d8f05SLen Brown 			continue;
29230e2d8f05SLen Brown 		fclose(filep);
29240e2d8f05SLen Brown 		return i;
29250e2d8f05SLen Brown 	}
29260e2d8f05SLen Brown 	return -1;
2927e275b388SDasaratharaman Chandramouli }
2928c98d5d94SLen Brown 
29290e2d8f05SLen Brown int get_thread_siblings(struct cpu_topology *thiscpu)
29300e2d8f05SLen Brown {
29310e2d8f05SLen Brown 	char path[80], character;
29320e2d8f05SLen Brown 	FILE *filep;
29330e2d8f05SLen Brown 	unsigned long map;
29348cb48b32SPrarit Bhargava 	int so, shift, sib_core;
29350e2d8f05SLen Brown 	int cpu = thiscpu->logical_cpu_id;
29360e2d8f05SLen Brown 	int offset = topo.max_cpu_num + 1;
29370e2d8f05SLen Brown 	size_t size;
29388cb48b32SPrarit Bhargava 	int thread_id = 0;
29390e2d8f05SLen Brown 
29400e2d8f05SLen Brown 	thiscpu->put_ids = CPU_ALLOC((topo.max_cpu_num + 1));
29418cb48b32SPrarit Bhargava 	if (thiscpu->thread_id < 0)
29428cb48b32SPrarit Bhargava 		thiscpu->thread_id = thread_id++;
29430e2d8f05SLen Brown 	if (!thiscpu->put_ids)
29440e2d8f05SLen Brown 		return -1;
29450e2d8f05SLen Brown 
29460e2d8f05SLen Brown 	size = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
29470e2d8f05SLen Brown 	CPU_ZERO_S(size, thiscpu->put_ids);
29480e2d8f05SLen Brown 
29491b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", cpu);
29503d7772eaSLen Brown 	filep = fopen(path, "r");
29513d7772eaSLen Brown 
29523d7772eaSLen Brown 	if (!filep) {
29533d7772eaSLen Brown 		warnx("%s: open failed", path);
29543d7772eaSLen Brown 		return -1;
29553d7772eaSLen Brown 	}
29560e2d8f05SLen Brown 	do {
29570e2d8f05SLen Brown 		offset -= BITMASK_SIZE;
29588173c336SBen Hutchings 		if (fscanf(filep, "%lx%c", &map, &character) != 2)
29598173c336SBen Hutchings 			err(1, "%s: failed to parse file", path);
29600e2d8f05SLen Brown 		for (shift = 0; shift < BITMASK_SIZE; shift++) {
29610e2d8f05SLen Brown 			if ((map >> shift) & 0x1) {
29628cb48b32SPrarit Bhargava 				so = shift + offset;
29638cb48b32SPrarit Bhargava 				sib_core = get_core_id(so);
29648cb48b32SPrarit Bhargava 				if (sib_core == thiscpu->physical_core_id) {
29658cb48b32SPrarit Bhargava 					CPU_SET_S(so, size, thiscpu->put_ids);
29661b439f01SLen Brown 					if ((so != cpu) && (cpus[so].thread_id < 0))
29671b439f01SLen Brown 						cpus[so].thread_id = thread_id++;
29688cb48b32SPrarit Bhargava 				}
29690e2d8f05SLen Brown 			}
29700e2d8f05SLen Brown 		}
2971e13da9a1SColin Ian King 	} while (character == ',');
2972c98d5d94SLen Brown 	fclose(filep);
29730e2d8f05SLen Brown 
29740e2d8f05SLen Brown 	return CPU_COUNT_S(size, thiscpu->put_ids);
2975c98d5d94SLen Brown }
2976c98d5d94SLen Brown 
2977c98d5d94SLen Brown /*
2978c98d5d94SLen Brown  * run func(thread, core, package) in topology order
2979c98d5d94SLen Brown  * skip non-present cpus
2980103a8feaSLen Brown  */
2981103a8feaSLen Brown 
2982c98d5d94SLen Brown int for_all_cpus_2(int (func) (struct thread_data *, struct core_data *,
2983c98d5d94SLen Brown 			       struct pkg_data *, struct thread_data *, struct core_data *,
2984c98d5d94SLen Brown 			       struct pkg_data *), struct thread_data *thread_base,
2985c98d5d94SLen Brown 		   struct core_data *core_base, struct pkg_data *pkg_base,
29861b439f01SLen Brown 		   struct thread_data *thread_base2, struct core_data *core_base2, struct pkg_data *pkg_base2)
2987c98d5d94SLen Brown {
298840f5cfe7SPrarit Bhargava 	int retval, pkg_no, node_no, core_no, thread_no;
2989c98d5d94SLen Brown 
2990c98d5d94SLen Brown 	for (pkg_no = 0; pkg_no < topo.num_packages; ++pkg_no) {
299140f5cfe7SPrarit Bhargava 		for (node_no = 0; node_no < topo.nodes_per_pkg; ++node_no) {
29921b439f01SLen Brown 			for (core_no = 0; core_no < topo.cores_per_node; ++core_no) {
29931b439f01SLen Brown 				for (thread_no = 0; thread_no < topo.threads_per_core; ++thread_no) {
2994c98d5d94SLen Brown 					struct thread_data *t, *t2;
2995c98d5d94SLen Brown 					struct core_data *c, *c2;
2996c98d5d94SLen Brown 					struct pkg_data *p, *p2;
2997c98d5d94SLen Brown 
29981b439f01SLen Brown 					t = GET_THREAD(thread_base, thread_no, core_no, node_no, pkg_no);
2999c98d5d94SLen Brown 
3000c98d5d94SLen Brown 					if (cpu_is_not_present(t->cpu_id))
3001c98d5d94SLen Brown 						continue;
3002c98d5d94SLen Brown 
30031b439f01SLen Brown 					t2 = GET_THREAD(thread_base2, thread_no, core_no, node_no, pkg_no);
3004c98d5d94SLen Brown 
30051b439f01SLen Brown 					c = GET_CORE(core_base, core_no, node_no, pkg_no);
30061b439f01SLen Brown 					c2 = GET_CORE(core_base2, core_no, node_no, pkg_no);
3007c98d5d94SLen Brown 
3008c98d5d94SLen Brown 					p = GET_PKG(pkg_base, pkg_no);
3009c98d5d94SLen Brown 					p2 = GET_PKG(pkg_base2, pkg_no);
3010c98d5d94SLen Brown 
3011c98d5d94SLen Brown 					retval = func(t, c, p, t2, c2, p2);
3012c98d5d94SLen Brown 					if (retval)
3013c98d5d94SLen Brown 						return retval;
3014c98d5d94SLen Brown 				}
3015c98d5d94SLen Brown 			}
3016c98d5d94SLen Brown 		}
301740f5cfe7SPrarit Bhargava 	}
3018c98d5d94SLen Brown 	return 0;
3019c98d5d94SLen Brown }
3020c98d5d94SLen Brown 
3021c98d5d94SLen Brown /*
3022c98d5d94SLen Brown  * run func(cpu) on every cpu in /proc/stat
3023c98d5d94SLen Brown  * return max_cpu number
3024c98d5d94SLen Brown  */
3025c98d5d94SLen Brown int for_all_proc_cpus(int (func) (int))
3026103a8feaSLen Brown {
3027103a8feaSLen Brown 	FILE *fp;
3028c98d5d94SLen Brown 	int cpu_num;
3029103a8feaSLen Brown 	int retval;
3030103a8feaSLen Brown 
303157a42a34SJosh Triplett 	fp = fopen_or_die(proc_stat, "r");
3032103a8feaSLen Brown 
3033103a8feaSLen Brown 	retval = fscanf(fp, "cpu %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n");
3034b2c95d90SJosh Triplett 	if (retval != 0)
3035b2c95d90SJosh Triplett 		err(1, "%s: failed to parse format", proc_stat);
3036103a8feaSLen Brown 
3037c98d5d94SLen Brown 	while (1) {
3038c98d5d94SLen Brown 		retval = fscanf(fp, "cpu%u %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d\n", &cpu_num);
3039103a8feaSLen Brown 		if (retval != 1)
3040103a8feaSLen Brown 			break;
3041103a8feaSLen Brown 
3042c98d5d94SLen Brown 		retval = func(cpu_num);
3043c98d5d94SLen Brown 		if (retval) {
3044c98d5d94SLen Brown 			fclose(fp);
3045c98d5d94SLen Brown 			return (retval);
3046c98d5d94SLen Brown 		}
3047103a8feaSLen Brown 	}
3048103a8feaSLen Brown 	fclose(fp);
3049c98d5d94SLen Brown 	return 0;
3050103a8feaSLen Brown }
3051103a8feaSLen Brown 
3052103a8feaSLen Brown void re_initialize(void)
3053103a8feaSLen Brown {
3054c98d5d94SLen Brown 	free_all_buffers();
3055c98d5d94SLen Brown 	setup_all_buffers();
30563d7772eaSLen Brown 	fprintf(outf, "turbostat: re-initialized with num_cpus %d\n", topo.num_cpus);
3057103a8feaSLen Brown }
3058103a8feaSLen Brown 
3059843c5791SPrarit Bhargava void set_max_cpu_num(void)
3060843c5791SPrarit Bhargava {
3061843c5791SPrarit Bhargava 	FILE *filep;
30628201a028SPrarit Bhargava 	int base_cpu;
3063843c5791SPrarit Bhargava 	unsigned long dummy;
30648201a028SPrarit Bhargava 	char pathname[64];
3065843c5791SPrarit Bhargava 
30668201a028SPrarit Bhargava 	base_cpu = sched_getcpu();
30678201a028SPrarit Bhargava 	if (base_cpu < 0)
30688201a028SPrarit Bhargava 		err(1, "cannot find calling cpu ID");
30691b439f01SLen Brown 	sprintf(pathname, "/sys/devices/system/cpu/cpu%d/topology/thread_siblings", base_cpu);
30708201a028SPrarit Bhargava 
30718201a028SPrarit Bhargava 	filep = fopen_or_die(pathname, "r");
3072843c5791SPrarit Bhargava 	topo.max_cpu_num = 0;
3073843c5791SPrarit Bhargava 	while (fscanf(filep, "%lx,", &dummy) == 1)
30740e2d8f05SLen Brown 		topo.max_cpu_num += BITMASK_SIZE;
3075843c5791SPrarit Bhargava 	fclose(filep);
3076843c5791SPrarit Bhargava 	topo.max_cpu_num--;	/* 0 based */
3077843c5791SPrarit Bhargava }
3078c98d5d94SLen Brown 
3079103a8feaSLen Brown /*
3080c98d5d94SLen Brown  * count_cpus()
3081c98d5d94SLen Brown  * remember the last one seen, it will be the max
3082103a8feaSLen Brown  */
3083c98d5d94SLen Brown int count_cpus(int cpu)
3084103a8feaSLen Brown {
30859878bf7aSLen Brown 	UNUSED(cpu);
30869878bf7aSLen 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 
34219878bf7aSLen Brown 	UNUSED(c);
34229878bf7aSLen Brown 	UNUSED(p);
34239878bf7aSLen Brown 
342487e15da9SChen Yu 	for (i = IDX_PKG_ENERGY; i < IDX_COUNT; i++) {
342587e15da9SChen Yu 		unsigned long long msr_cur, msr_last;
342613a779deSCalvin Walton 		off_t offset;
342787e15da9SChen Yu 
342887e15da9SChen Yu 		if (!idx_valid(i))
342987e15da9SChen Yu 			continue;
343087e15da9SChen Yu 		offset = idx_to_offset(i);
343187e15da9SChen Yu 		if (offset < 0)
343287e15da9SChen Yu 			continue;
343387e15da9SChen Yu 		ret = get_msr(cpu, offset, &msr_cur);
343487e15da9SChen Yu 		if (ret) {
34351b439f01SLen Brown 			fprintf(outf, "Can not update msr(0x%llx)\n", (unsigned long long)offset);
343687e15da9SChen Yu 			continue;
343787e15da9SChen Yu 		}
343887e15da9SChen Yu 
343987e15da9SChen Yu 		msr_last = per_cpu_msr_sum[cpu].entries[i].last;
344087e15da9SChen Yu 		per_cpu_msr_sum[cpu].entries[i].last = msr_cur & 0xffffffff;
344187e15da9SChen Yu 
344287e15da9SChen Yu 		DELTA_WRAP32(msr_cur, msr_last);
344387e15da9SChen Yu 		per_cpu_msr_sum[cpu].entries[i].sum += msr_last;
344487e15da9SChen Yu 	}
344587e15da9SChen Yu 	return 0;
344687e15da9SChen Yu }
344787e15da9SChen Yu 
34481b439f01SLen Brown static void msr_record_handler(union sigval v)
344987e15da9SChen Yu {
34509878bf7aSLen Brown 	UNUSED(v);
34519878bf7aSLen Brown 
345287e15da9SChen Yu 	for_all_cpus(update_msr_sum, EVEN_COUNTERS);
345387e15da9SChen Yu }
345487e15da9SChen Yu 
345587e15da9SChen Yu void msr_sum_record(void)
345687e15da9SChen Yu {
345787e15da9SChen Yu 	struct itimerspec its;
345887e15da9SChen Yu 	struct sigevent sev;
345987e15da9SChen Yu 
346087e15da9SChen Yu 	per_cpu_msr_sum = calloc(topo.max_cpu_num + 1, sizeof(struct msr_sum_array));
346187e15da9SChen Yu 	if (!per_cpu_msr_sum) {
346287e15da9SChen Yu 		fprintf(outf, "Can not allocate memory for long time MSR.\n");
346387e15da9SChen Yu 		return;
346487e15da9SChen Yu 	}
346587e15da9SChen Yu 	/*
346687e15da9SChen Yu 	 * Signal handler might be restricted, so use thread notifier instead.
346787e15da9SChen Yu 	 */
346887e15da9SChen Yu 	memset(&sev, 0, sizeof(struct sigevent));
346987e15da9SChen Yu 	sev.sigev_notify = SIGEV_THREAD;
347087e15da9SChen Yu 	sev.sigev_notify_function = msr_record_handler;
347187e15da9SChen Yu 
347287e15da9SChen Yu 	sev.sigev_value.sival_ptr = &timerid;
347387e15da9SChen Yu 	if (timer_create(CLOCK_REALTIME, &sev, &timerid) == -1) {
347487e15da9SChen Yu 		fprintf(outf, "Can not create timer.\n");
347587e15da9SChen Yu 		goto release_msr;
347687e15da9SChen Yu 	}
347787e15da9SChen Yu 
347887e15da9SChen Yu 	its.it_value.tv_sec = 0;
347987e15da9SChen Yu 	its.it_value.tv_nsec = 1;
348087e15da9SChen Yu 	/*
348187e15da9SChen Yu 	 * A wraparound time has been calculated early.
348287e15da9SChen Yu 	 * Some sources state that the peak power for a
348387e15da9SChen Yu 	 * microprocessor is usually 1.5 times the TDP rating,
348487e15da9SChen Yu 	 * use 2 * TDP for safety.
348587e15da9SChen Yu 	 */
348687e15da9SChen Yu 	its.it_interval.tv_sec = rapl_joule_counter_range / 2;
348787e15da9SChen Yu 	its.it_interval.tv_nsec = 0;
348887e15da9SChen Yu 
348987e15da9SChen Yu 	if (timer_settime(timerid, 0, &its, NULL) == -1) {
349087e15da9SChen Yu 		fprintf(outf, "Can not set timer.\n");
349187e15da9SChen Yu 		goto release_timer;
349287e15da9SChen Yu 	}
349387e15da9SChen Yu 	return;
349487e15da9SChen Yu 
349587e15da9SChen Yu release_timer:
349687e15da9SChen Yu 	timer_delete(timerid);
349787e15da9SChen Yu release_msr:
349887e15da9SChen Yu 	free(per_cpu_msr_sum);
349987e15da9SChen Yu }
35004c2122d4SLen Brown 
350138c6663aSLen Brown /*
350238c6663aSLen Brown  * set_my_sched_priority(pri)
350338c6663aSLen Brown  * return previous
3504164d7a96SLen Brown  *
3505164d7a96SLen Brown  * if non-root, do this:
3506164d7a96SLen Brown  * # /sbin/setcap cap_sys_rawio,cap_sys_nice=+ep /usr/bin/turbostat
350738c6663aSLen Brown  */
350838c6663aSLen Brown int set_my_sched_priority(int priority)
350938c6663aSLen Brown {
351038c6663aSLen Brown 	int retval;
351138c6663aSLen Brown 	int original_priority;
351238c6663aSLen Brown 
351338c6663aSLen Brown 	errno = 0;
351438c6663aSLen Brown 	original_priority = getpriority(PRIO_PROCESS, 0);
351538c6663aSLen Brown 	if (errno && (original_priority == -1))
351638c6663aSLen Brown 		err(errno, "getpriority");
351738c6663aSLen Brown 
351838c6663aSLen Brown 	retval = setpriority(PRIO_PROCESS, 0, priority);
351938c6663aSLen Brown 	if (retval)
352038c6663aSLen Brown 		err(retval, "setpriority(%d)", priority);
352138c6663aSLen Brown 
352238c6663aSLen Brown 	errno = 0;
352338c6663aSLen Brown 	retval = getpriority(PRIO_PROCESS, 0);
352438c6663aSLen Brown 	if (retval != priority)
3525164d7a96SLen Brown 		err(retval, "getpriority(%d) != setpriority(%d)", retval, priority);
352638c6663aSLen Brown 
352738c6663aSLen Brown 	return original_priority;
352838c6663aSLen Brown }
352938c6663aSLen Brown 
3530103a8feaSLen Brown void turbostat_loop()
3531103a8feaSLen Brown {
3532c98d5d94SLen Brown 	int retval;
3533e52966c0SLen Brown 	int restarted = 0;
35349878bf7aSLen Brown 	unsigned int done_iters = 0;
3535c98d5d94SLen Brown 
35368aa2ed0bSLen Brown 	setup_signal_handler();
35378aa2ed0bSLen Brown 
353838c6663aSLen Brown 	/*
353938c6663aSLen Brown 	 * elevate own priority for interval mode
354038c6663aSLen Brown 	 */
354138c6663aSLen Brown 	set_my_sched_priority(-20);
354238c6663aSLen Brown 
3543103a8feaSLen Brown restart:
3544e52966c0SLen Brown 	restarted++;
3545e52966c0SLen Brown 
3546562a2d37SLen Brown 	snapshot_proc_sysfs_files();
3547c98d5d94SLen Brown 	retval = for_all_cpus(get_counters, EVEN_COUNTERS);
35484c2122d4SLen Brown 	first_counter_read = 0;
3549d91bb17cSLen Brown 	if (retval < -1) {
3550d91bb17cSLen Brown 		exit(retval);
3551d91bb17cSLen Brown 	} else if (retval == -1) {
35523d7772eaSLen Brown 		if (restarted > 10) {
3553e52966c0SLen Brown 			exit(retval);
3554e52966c0SLen Brown 		}
3555c98d5d94SLen Brown 		re_initialize();
3556c98d5d94SLen Brown 		goto restart;
3557c98d5d94SLen Brown 	}
3558e52966c0SLen Brown 	restarted = 0;
3559023fe0acSChen Yu 	done_iters = 0;
3560103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
3561103a8feaSLen Brown 
3562103a8feaSLen Brown 	while (1) {
3563c98d5d94SLen Brown 		if (for_all_proc_cpus(cpu_is_not_present)) {
3564103a8feaSLen Brown 			re_initialize();
3565103a8feaSLen Brown 			goto restart;
3566103a8feaSLen Brown 		}
3567b9ad8ee0SLen Brown 		do_sleep();
3568562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3569562a2d37SLen Brown 			goto restart;
3570c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, ODD_COUNTERS);
3571d91bb17cSLen Brown 		if (retval < -1) {
3572d91bb17cSLen Brown 			exit(retval);
3573d91bb17cSLen Brown 		} else if (retval == -1) {
357415aaa346SLen Brown 			re_initialize();
357515aaa346SLen Brown 			goto restart;
357615aaa346SLen Brown 		}
3577103a8feaSLen Brown 		gettimeofday(&tv_odd, (struct timezone *)NULL);
3578103a8feaSLen Brown 		timersub(&tv_odd, &tv_even, &tv_delta);
3579ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS)) {
3580ba3dec99SLen Brown 			re_initialize();
3581ba3dec99SLen Brown 			goto restart;
3582ba3dec99SLen Brown 		}
3583c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
3584c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
3585b7d8c148SLen Brown 		flush_output_stdout();
35868aa2ed0bSLen Brown 		if (exit_requested)
35878aa2ed0bSLen Brown 			break;
3588023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3589023fe0acSChen Yu 			break;
3590b9ad8ee0SLen Brown 		do_sleep();
3591562a2d37SLen Brown 		if (snapshot_proc_sysfs_files())
3592562a2d37SLen Brown 			goto restart;
3593c98d5d94SLen Brown 		retval = for_all_cpus(get_counters, EVEN_COUNTERS);
3594d91bb17cSLen Brown 		if (retval < -1) {
3595d91bb17cSLen Brown 			exit(retval);
3596d91bb17cSLen Brown 		} else if (retval == -1) {
3597103a8feaSLen Brown 			re_initialize();
3598103a8feaSLen Brown 			goto restart;
3599103a8feaSLen Brown 		}
3600103a8feaSLen Brown 		gettimeofday(&tv_even, (struct timezone *)NULL);
3601103a8feaSLen Brown 		timersub(&tv_even, &tv_odd, &tv_delta);
3602ba3dec99SLen Brown 		if (for_all_cpus_2(delta_cpu, EVEN_COUNTERS, ODD_COUNTERS)) {
3603ba3dec99SLen Brown 			re_initialize();
3604ba3dec99SLen Brown 			goto restart;
3605ba3dec99SLen Brown 		}
3606c98d5d94SLen Brown 		compute_average(ODD_COUNTERS);
3607c98d5d94SLen Brown 		format_all_counters(ODD_COUNTERS);
3608b7d8c148SLen Brown 		flush_output_stdout();
36098aa2ed0bSLen Brown 		if (exit_requested)
36108aa2ed0bSLen Brown 			break;
3611023fe0acSChen Yu 		if (num_iterations && ++done_iters >= num_iterations)
3612023fe0acSChen Yu 			break;
3613103a8feaSLen Brown 	}
3614103a8feaSLen Brown }
3615103a8feaSLen Brown 
3616103a8feaSLen Brown void check_dev_msr()
3617103a8feaSLen Brown {
3618103a8feaSLen Brown 	struct stat sb;
36197ce7d5deSPrarit Bhargava 	char pathname[32];
3620103a8feaSLen Brown 
36217ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36227ce7d5deSPrarit Bhargava 	if (stat(pathname, &sb))
3623a21d38c8SLen Brown 		if (system("/sbin/modprobe msr > /dev/null 2>&1"))
3624d7899447SLen Brown 			err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
3625103a8feaSLen Brown }
3626103a8feaSLen Brown 
3627fcaa681cSLen Brown /*
3628fcaa681cSLen Brown  * check for CAP_SYS_RAWIO
3629fcaa681cSLen Brown  * return 0 on success
3630fcaa681cSLen Brown  * return 1 on fail
3631fcaa681cSLen Brown  */
3632fcaa681cSLen Brown int check_for_cap_sys_rawio(void)
3633103a8feaSLen Brown {
3634fcaa681cSLen Brown 	cap_t caps;
3635fcaa681cSLen Brown 	cap_flag_value_t cap_flag_value;
3636fcaa681cSLen Brown 
3637fcaa681cSLen Brown 	caps = cap_get_proc();
3638fcaa681cSLen Brown 	if (caps == NULL)
3639fcaa681cSLen Brown 		err(-6, "cap_get_proc\n");
3640fcaa681cSLen Brown 
3641fcaa681cSLen Brown 	if (cap_get_flag(caps, CAP_SYS_RAWIO, CAP_EFFECTIVE, &cap_flag_value))
3642fcaa681cSLen Brown 		err(-6, "cap_get\n");
3643fcaa681cSLen Brown 
3644fcaa681cSLen Brown 	if (cap_flag_value != CAP_SET) {
36451b439f01SLen Brown 		warnx("capget(CAP_SYS_RAWIO) failed," " try \"# setcap cap_sys_rawio=ep %s\"", progname);
3646fcaa681cSLen Brown 		return 1;
3647fcaa681cSLen Brown 	}
3648fcaa681cSLen Brown 
3649fcaa681cSLen Brown 	if (cap_free(caps) == -1)
3650fcaa681cSLen Brown 		err(-6, "cap_free\n");
3651fcaa681cSLen Brown 
3652fcaa681cSLen Brown 	return 0;
3653fcaa681cSLen Brown }
36541b439f01SLen Brown 
3655fcaa681cSLen Brown void check_permissions(void)
3656fcaa681cSLen Brown {
365798481e79SLen Brown 	int do_exit = 0;
36587ce7d5deSPrarit Bhargava 	char pathname[32];
365998481e79SLen Brown 
366098481e79SLen Brown 	/* check for CAP_SYS_RAWIO */
3661fcaa681cSLen Brown 	do_exit += check_for_cap_sys_rawio();
366298481e79SLen Brown 
366398481e79SLen Brown 	/* test file permissions */
36647ce7d5deSPrarit Bhargava 	sprintf(pathname, "/dev/cpu/%d/msr", base_cpu);
36657ce7d5deSPrarit Bhargava 	if (euidaccess(pathname, R_OK)) {
366698481e79SLen Brown 		do_exit++;
366798481e79SLen Brown 		warn("/dev/cpu/0/msr open failed, try chown or chmod +r /dev/cpu/*/msr");
366898481e79SLen Brown 	}
366998481e79SLen Brown 
367098481e79SLen Brown 	/* if all else fails, thell them to be root */
367198481e79SLen Brown 	if (do_exit)
3672b2c95d90SJosh Triplett 		if (getuid() != 0)
3673d7899447SLen Brown 			warnx("... or simply run as root");
367498481e79SLen Brown 
367598481e79SLen Brown 	if (do_exit)
367698481e79SLen Brown 		exit(-6);
3677103a8feaSLen Brown }
3678103a8feaSLen Brown 
3679d7899447SLen Brown /*
3680d7899447SLen Brown  * NHM adds support for additional MSRs:
3681d7899447SLen Brown  *
3682d7899447SLen Brown  * MSR_SMI_COUNT                   0x00000034
3683d7899447SLen Brown  *
3684ec0adc53SLen Brown  * MSR_PLATFORM_INFO               0x000000ce
36851df2e55aSLen Brown  * MSR_PKG_CST_CONFIG_CONTROL     0x000000e2
3686d7899447SLen Brown  *
3687cf4cbe53SLen Brown  * MSR_MISC_PWR_MGMT               0x000001aa
3688cf4cbe53SLen Brown  *
3689d7899447SLen Brown  * MSR_PKG_C3_RESIDENCY            0x000003f8
3690d7899447SLen Brown  * MSR_PKG_C6_RESIDENCY            0x000003f9
3691d7899447SLen Brown  * MSR_CORE_C3_RESIDENCY           0x000003fc
3692d7899447SLen Brown  * MSR_CORE_C6_RESIDENCY           0x000003fd
3693d7899447SLen Brown  *
3694ee7e38e3SLen Brown  * Side effect:
36951df2e55aSLen Brown  * sets global pkg_cstate_limit to decode MSR_PKG_CST_CONFIG_CONTROL
369633148d67SLen Brown  * sets has_misc_feature_control
3697d7899447SLen Brown  */
3698ee7e38e3SLen Brown int probe_nhm_msrs(unsigned int family, unsigned int model)
3699103a8feaSLen Brown {
3700ee7e38e3SLen Brown 	unsigned long long msr;
370121ed5574SLen Brown 	unsigned int base_ratio;
3702ee7e38e3SLen Brown 	int *pkg_cstate_limits;
3703ee7e38e3SLen Brown 
3704103a8feaSLen Brown 	if (!genuine_intel)
3705103a8feaSLen Brown 		return 0;
3706103a8feaSLen Brown 
3707103a8feaSLen Brown 	if (family != 6)
3708103a8feaSLen Brown 		return 0;
3709103a8feaSLen Brown 
371021ed5574SLen Brown 	bclk = discover_bclk(family, model);
371121ed5574SLen Brown 
3712103a8feaSLen Brown 	switch (model) {
3713869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
3714869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3715ee7e38e3SLen Brown 		pkg_cstate_limits = nhm_pkg_cstate_limits;
3716ee7e38e3SLen Brown 		break;
3717869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:	/* SNB */
3718869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:	/* SNB Xeon */
3719869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
3720869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3721ee7e38e3SLen Brown 		pkg_cstate_limits = snb_pkg_cstate_limits;
372233148d67SLen Brown 		has_misc_feature_control = 1;
3723ee7e38e3SLen Brown 		break;
3724c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
37255e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
3726869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
372777e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
3728c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
37295e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
3730869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
3731af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
3732af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
3733ee7e38e3SLen Brown 		pkg_cstate_limits = hsw_pkg_cstate_limits;
373433148d67SLen Brown 		has_misc_feature_control = 1;
3735ee7e38e3SLen Brown 		break;
3736d8ebb442SLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
3737*0e4d42afSArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:	/* SPR */
3738d8ebb442SLen Brown 		pkg_cstate_limits = skx_pkg_cstate_limits;
373933148d67SLen Brown 		has_misc_feature_control = 1;
3740d8ebb442SLen Brown 		break;
374125368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
374225368d7cSChen Yu 		pkg_cstate_limits = icx_pkg_cstate_limits;
374325368d7cSChen Yu 		has_misc_feature_control = 1;
374425368d7cSChen Yu 		break;
3745f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
3746cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
37479878bf7aSLen Brown 		/* FALLTHRU */
37485ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
3749ee7e38e3SLen Brown 		pkg_cstate_limits = slv_pkg_cstate_limits;
3750ee7e38e3SLen Brown 		break;
3751869ce69eSLen Brown 	case INTEL_FAM6_ATOM_AIRMONT:	/* AMT */
3752ee7e38e3SLen Brown 		pkg_cstate_limits = amt_pkg_cstate_limits;
3753cf4cbe53SLen Brown 		no_MSR_MISC_PWR_MGMT = 1;
3754ee7e38e3SLen Brown 		break;
3755869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI */
3756ee7e38e3SLen Brown 		pkg_cstate_limits = phi_pkg_cstate_limits;
3757ee7e38e3SLen Brown 		break;
3758869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
3759f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
37605ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
3761f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
376220de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
3763445640a5SLen Brown 		pkg_cstate_limits = glm_pkg_cstate_limits;
3764e4085d54SLen Brown 		break;
3765103a8feaSLen Brown 	default:
3766103a8feaSLen Brown 		return 0;
3767103a8feaSLen Brown 	}
37681df2e55aSLen Brown 	get_msr(base_cpu, MSR_PKG_CST_CONFIG_CONTROL, &msr);
3769e9257f5fSLen Brown 	pkg_cstate_limit = pkg_cstate_limits[msr & 0xF];
3770ee7e38e3SLen Brown 
3771ec0adc53SLen Brown 	get_msr(base_cpu, MSR_PLATFORM_INFO, &msr);
377221ed5574SLen Brown 	base_ratio = (msr >> 8) & 0xFF;
377321ed5574SLen Brown 
377421ed5574SLen Brown 	base_hz = base_ratio * bclk * 1000000;
377521ed5574SLen Brown 	has_base_hz = 1;
3776ee7e38e3SLen Brown 	return 1;
3777103a8feaSLen Brown }
37781b439f01SLen Brown 
37790f7887c4SLen Brown /*
3780495c7654SLen Brown  * SLV client has support for unique MSRs:
37810f7887c4SLen Brown  *
37820f7887c4SLen Brown  * MSR_CC6_DEMOTION_POLICY_CONFIG
37830f7887c4SLen Brown  * MSR_MC6_DEMOTION_POLICY_CONFIG
37840f7887c4SLen Brown  */
37850f7887c4SLen Brown 
37860f7887c4SLen Brown int has_slv_msrs(unsigned int family, unsigned int model)
37870f7887c4SLen Brown {
37880f7887c4SLen Brown 	if (!genuine_intel)
37890f7887c4SLen Brown 		return 0;
37900f7887c4SLen Brown 
37919878bf7aSLen Brown 	if (family != 6)
37929878bf7aSLen Brown 		return 0;
37939878bf7aSLen Brown 
37940f7887c4SLen Brown 	switch (model) {
3795f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
3796f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_MID:
3797f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_AIRMONT_MID:
37980f7887c4SLen Brown 		return 1;
37990f7887c4SLen Brown 	}
38000f7887c4SLen Brown 	return 0;
38010f7887c4SLen Brown }
38021b439f01SLen Brown 
38037170a374SLen Brown int is_dnv(unsigned int family, unsigned int model)
38047170a374SLen Brown {
38057170a374SLen Brown 
38067170a374SLen Brown 	if (!genuine_intel)
38077170a374SLen Brown 		return 0;
38087170a374SLen Brown 
38099878bf7aSLen Brown 	if (family != 6)
38109878bf7aSLen Brown 		return 0;
38119878bf7aSLen Brown 
38127170a374SLen Brown 	switch (model) {
38135ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:
38147170a374SLen Brown 		return 1;
38157170a374SLen Brown 	}
38167170a374SLen Brown 	return 0;
38177170a374SLen Brown }
38181b439f01SLen Brown 
3819ade0ebacSLen Brown int is_bdx(unsigned int family, unsigned int model)
3820ade0ebacSLen Brown {
3821ade0ebacSLen Brown 
3822ade0ebacSLen Brown 	if (!genuine_intel)
3823ade0ebacSLen Brown 		return 0;
3824ade0ebacSLen Brown 
38259878bf7aSLen Brown 	if (family != 6)
38269878bf7aSLen Brown 		return 0;
38279878bf7aSLen Brown 
3828ade0ebacSLen Brown 	switch (model) {
3829ade0ebacSLen Brown 	case INTEL_FAM6_BROADWELL_X:
3830ade0ebacSLen Brown 		return 1;
3831ade0ebacSLen Brown 	}
3832ade0ebacSLen Brown 	return 0;
3833ade0ebacSLen Brown }
38341b439f01SLen Brown 
383534c76197SLen Brown int is_skx(unsigned int family, unsigned int model)
383634c76197SLen Brown {
383734c76197SLen Brown 
383834c76197SLen Brown 	if (!genuine_intel)
383934c76197SLen Brown 		return 0;
384034c76197SLen Brown 
38419878bf7aSLen Brown 	if (family != 6)
38429878bf7aSLen Brown 		return 0;
38439878bf7aSLen Brown 
384434c76197SLen Brown 	switch (model) {
384534c76197SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
384634c76197SLen Brown 		return 1;
384734c76197SLen Brown 	}
384834c76197SLen Brown 	return 0;
384934c76197SLen Brown }
385025368d7cSChen Yu 
385125368d7cSChen Yu int is_icx(unsigned int family, unsigned int model)
385225368d7cSChen Yu {
385325368d7cSChen Yu 
385425368d7cSChen Yu 	if (!genuine_intel)
385525368d7cSChen Yu 		return 0;
385625368d7cSChen Yu 
38579878bf7aSLen Brown 	if (family != 6)
38589878bf7aSLen Brown 		return 0;
38599878bf7aSLen Brown 
386025368d7cSChen Yu 	switch (model) {
386125368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
386225368d7cSChen Yu 		return 1;
386325368d7cSChen Yu 	}
386425368d7cSChen Yu 	return 0;
386525368d7cSChen Yu }
386625368d7cSChen Yu 
3867684e40e9SArtem Bityutskiy int is_spr(unsigned int family, unsigned int model)
3868684e40e9SArtem Bityutskiy {
3869684e40e9SArtem Bityutskiy 
3870684e40e9SArtem Bityutskiy 	if (!genuine_intel)
3871684e40e9SArtem Bityutskiy 		return 0;
3872684e40e9SArtem Bityutskiy 
3873684e40e9SArtem Bityutskiy 	if (family != 6)
3874684e40e9SArtem Bityutskiy 		return 0;
3875684e40e9SArtem Bityutskiy 
3876684e40e9SArtem Bityutskiy 	switch (model) {
3877684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:
3878684e40e9SArtem Bityutskiy 		return 1;
3879684e40e9SArtem Bityutskiy 	}
3880684e40e9SArtem Bityutskiy 	return 0;
3881684e40e9SArtem Bityutskiy }
3882684e40e9SArtem Bityutskiy 
3883f6708400SChen Yu int is_ehl(unsigned int family, unsigned int model)
3884f6708400SChen Yu {
3885f6708400SChen Yu 	if (!genuine_intel)
3886f6708400SChen Yu 		return 0;
3887f6708400SChen Yu 
38889878bf7aSLen Brown 	if (family != 6)
38899878bf7aSLen Brown 		return 0;
38909878bf7aSLen Brown 
3891f6708400SChen Yu 	switch (model) {
3892f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:
3893f6708400SChen Yu 		return 1;
3894f6708400SChen Yu 	}
3895f6708400SChen Yu 	return 0;
3896f6708400SChen Yu }
38971b439f01SLen Brown 
389820de0dabSAntti Laakso int is_jvl(unsigned int family, unsigned int model)
389920de0dabSAntti Laakso {
390020de0dabSAntti Laakso 	if (!genuine_intel)
390120de0dabSAntti Laakso 		return 0;
390220de0dabSAntti Laakso 
39039878bf7aSLen Brown 	if (family != 6)
39049878bf7aSLen Brown 		return 0;
39059878bf7aSLen Brown 
390620de0dabSAntti Laakso 	switch (model) {
390720de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:
390820de0dabSAntti Laakso 		return 1;
390920de0dabSAntti Laakso 	}
391020de0dabSAntti Laakso 	return 0;
391120de0dabSAntti Laakso }
39120f7887c4SLen Brown 
391331e07522SLen Brown int has_turbo_ratio_limit(unsigned int family, unsigned int model)
3914d7899447SLen Brown {
39150f7887c4SLen Brown 	if (has_slv_msrs(family, model))
39160f7887c4SLen Brown 		return 0;
39170f7887c4SLen Brown 
39189878bf7aSLen Brown 	if (family != 6)
39199878bf7aSLen Brown 		return 0;
39209878bf7aSLen Brown 
3921d7899447SLen Brown 	switch (model) {
3922d7899447SLen Brown 		/* Nehalem compatible, but do not include turbo-ratio limit support */
3923869ce69eSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
3924869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* PHI - Knights Landing (different MSR definition) */
3925d7899447SLen Brown 		return 0;
3926d7899447SLen Brown 	default:
3927d7899447SLen Brown 		return 1;
3928d7899447SLen Brown 	}
3929d7899447SLen Brown }
39301b439f01SLen Brown 
39310f7887c4SLen Brown int has_atom_turbo_ratio_limit(unsigned int family, unsigned int model)
39320f7887c4SLen Brown {
39330f7887c4SLen Brown 	if (has_slv_msrs(family, model))
39340f7887c4SLen Brown 		return 1;
39350f7887c4SLen Brown 
39360f7887c4SLen Brown 	return 0;
39370f7887c4SLen Brown }
39381b439f01SLen Brown 
39396574a5d5SLen Brown int has_ivt_turbo_ratio_limit(unsigned int family, unsigned int model)
39406574a5d5SLen Brown {
39416574a5d5SLen Brown 	if (!genuine_intel)
39426574a5d5SLen Brown 		return 0;
39436574a5d5SLen Brown 
39446574a5d5SLen Brown 	if (family != 6)
39456574a5d5SLen Brown 		return 0;
39466574a5d5SLen Brown 
39476574a5d5SLen Brown 	switch (model) {
3948869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
3949869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
39506574a5d5SLen Brown 		return 1;
39516574a5d5SLen Brown 	default:
39526574a5d5SLen Brown 		return 0;
39536574a5d5SLen Brown 	}
39546574a5d5SLen Brown }
39551b439f01SLen Brown 
3956fcd17211SLen Brown int has_hsw_turbo_ratio_limit(unsigned int family, unsigned int model)
3957fcd17211SLen Brown {
3958fcd17211SLen Brown 	if (!genuine_intel)
3959fcd17211SLen Brown 		return 0;
3960fcd17211SLen Brown 
3961fcd17211SLen Brown 	if (family != 6)
3962fcd17211SLen Brown 		return 0;
3963fcd17211SLen Brown 
3964fcd17211SLen Brown 	switch (model) {
3965869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW Xeon */
3966fcd17211SLen Brown 		return 1;
3967fcd17211SLen Brown 	default:
3968fcd17211SLen Brown 		return 0;
3969fcd17211SLen Brown 	}
3970fcd17211SLen Brown }
3971fcd17211SLen Brown 
3972fb5d4327SDasaratharaman Chandramouli int has_knl_turbo_ratio_limit(unsigned int family, unsigned int model)
3973fb5d4327SDasaratharaman Chandramouli {
3974fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
3975fb5d4327SDasaratharaman Chandramouli 		return 0;
3976fb5d4327SDasaratharaman Chandramouli 
3977fb5d4327SDasaratharaman Chandramouli 	if (family != 6)
3978fb5d4327SDasaratharaman Chandramouli 		return 0;
3979fb5d4327SDasaratharaman Chandramouli 
3980fb5d4327SDasaratharaman Chandramouli 	switch (model) {
3981869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
3982fb5d4327SDasaratharaman Chandramouli 		return 1;
3983fb5d4327SDasaratharaman Chandramouli 	default:
3984fb5d4327SDasaratharaman Chandramouli 		return 0;
3985fb5d4327SDasaratharaman Chandramouli 	}
3986fb5d4327SDasaratharaman Chandramouli }
39871b439f01SLen Brown 
398831e07522SLen Brown int has_glm_turbo_ratio_limit(unsigned int family, unsigned int model)
398931e07522SLen Brown {
399031e07522SLen Brown 	if (!genuine_intel)
399131e07522SLen Brown 		return 0;
399231e07522SLen Brown 
399331e07522SLen Brown 	if (family != 6)
399431e07522SLen Brown 		return 0;
399531e07522SLen Brown 
399631e07522SLen Brown 	switch (model) {
399731e07522SLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:
399831e07522SLen Brown 	case INTEL_FAM6_SKYLAKE_X:
399925368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:
4000684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:
400131e07522SLen Brown 		return 1;
400231e07522SLen Brown 	default:
400331e07522SLen Brown 		return 0;
400431e07522SLen Brown 	}
400531e07522SLen Brown }
40061b439f01SLen Brown 
40076fb3143bSLen Brown int has_config_tdp(unsigned int family, unsigned int model)
40086fb3143bSLen Brown {
40096fb3143bSLen Brown 	if (!genuine_intel)
40106fb3143bSLen Brown 		return 0;
40116fb3143bSLen Brown 
40126fb3143bSLen Brown 	if (family != 6)
40136fb3143bSLen Brown 		return 0;
40146fb3143bSLen Brown 
40156fb3143bSLen Brown 	switch (model) {
4016869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
4017c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
4018869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
401977e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
40205e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
4021c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
40225e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
4023869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4024af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4025af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
4026869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
402725368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
4028684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:	/* SPR */
4029869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* Knights Landing */
40306fb3143bSLen Brown 		return 1;
40316fb3143bSLen Brown 	default:
40326fb3143bSLen Brown 		return 0;
40336fb3143bSLen Brown 	}
40346fb3143bSLen Brown }
40356fb3143bSLen Brown 
40360b9a0b9bSZhang Rui /*
40370b9a0b9bSZhang Rui  * tcc_offset_bits:
40380b9a0b9bSZhang Rui  * 0: Tcc Offset not supported (Default)
40390b9a0b9bSZhang Rui  * 6: Bit 29:24 of MSR_PLATFORM_INFO
40400b9a0b9bSZhang Rui  * 4: Bit 27:24 of MSR_PLATFORM_INFO
40410b9a0b9bSZhang Rui  */
40420b9a0b9bSZhang Rui void check_tcc_offset(int model)
40430b9a0b9bSZhang Rui {
40440b9a0b9bSZhang Rui 	unsigned long long msr;
40450b9a0b9bSZhang Rui 
40460b9a0b9bSZhang Rui 	if (!genuine_intel)
40470b9a0b9bSZhang Rui 		return;
40480b9a0b9bSZhang Rui 
40490b9a0b9bSZhang Rui 	switch (model) {
40500b9a0b9bSZhang Rui 	case INTEL_FAM6_SKYLAKE_L:
40510b9a0b9bSZhang Rui 	case INTEL_FAM6_SKYLAKE:
40520b9a0b9bSZhang Rui 	case INTEL_FAM6_KABYLAKE_L:
40530b9a0b9bSZhang Rui 	case INTEL_FAM6_KABYLAKE:
40540b9a0b9bSZhang Rui 	case INTEL_FAM6_ICELAKE_L:
40550b9a0b9bSZhang Rui 	case INTEL_FAM6_ICELAKE:
40560b9a0b9bSZhang Rui 	case INTEL_FAM6_TIGERLAKE_L:
40570b9a0b9bSZhang Rui 	case INTEL_FAM6_TIGERLAKE:
40580b9a0b9bSZhang Rui 	case INTEL_FAM6_COMETLAKE:
40590b9a0b9bSZhang Rui 		if (!get_msr(base_cpu, MSR_PLATFORM_INFO, &msr)) {
40600b9a0b9bSZhang Rui 			msr = (msr >> 30) & 1;
40610b9a0b9bSZhang Rui 			if (msr)
40620b9a0b9bSZhang Rui 				tcc_offset_bits = 6;
40630b9a0b9bSZhang Rui 		}
40640b9a0b9bSZhang Rui 		return;
40650b9a0b9bSZhang Rui 	default:
40660b9a0b9bSZhang Rui 		return;
40670b9a0b9bSZhang Rui 	}
40680b9a0b9bSZhang Rui }
40690b9a0b9bSZhang Rui 
40701b439f01SLen Brown static void remove_underbar(char *s)
4071fecb3bc8SDavid Arcari {
4072fecb3bc8SDavid Arcari 	char *to = s;
4073fecb3bc8SDavid Arcari 
4074fecb3bc8SDavid Arcari 	while (*s) {
4075fecb3bc8SDavid Arcari 		if (*s != '_')
4076fecb3bc8SDavid Arcari 			*to++ = *s;
4077fecb3bc8SDavid Arcari 		s++;
4078fecb3bc8SDavid Arcari 	}
4079fecb3bc8SDavid Arcari 
4080fecb3bc8SDavid Arcari 	*to = 0;
4081fecb3bc8SDavid Arcari }
4082fecb3bc8SDavid Arcari 
40831b439f01SLen Brown static void dump_cstate_pstate_config_info(unsigned int family, unsigned int model)
4084fcd17211SLen Brown {
4085fcd17211SLen Brown 	if (!do_nhm_platform_info)
4086fcd17211SLen Brown 		return;
4087fcd17211SLen Brown 
4088fcd17211SLen Brown 	dump_nhm_platform_info();
4089fcd17211SLen Brown 
4090fcd17211SLen Brown 	if (has_hsw_turbo_ratio_limit(family, model))
4091fcd17211SLen Brown 		dump_hsw_turbo_ratio_limits();
4092fcd17211SLen Brown 
4093fcd17211SLen Brown 	if (has_ivt_turbo_ratio_limit(family, model))
4094fcd17211SLen Brown 		dump_ivt_turbo_ratio_limits();
4095fcd17211SLen Brown 
40964af184eeSLen Brown 	if (has_turbo_ratio_limit(family, model)) {
40974af184eeSLen Brown 		dump_turbo_ratio_limits(MSR_TURBO_RATIO_LIMIT, family, model);
40984af184eeSLen Brown 
40994af184eeSLen Brown 		if (is_hybrid)
41004af184eeSLen Brown 			dump_turbo_ratio_limits(MSR_SECONDARY_TURBO_RATIO_LIMIT, family, model);
41014af184eeSLen Brown 	}
4102fcd17211SLen Brown 
41030f7887c4SLen Brown 	if (has_atom_turbo_ratio_limit(family, model))
41040f7887c4SLen Brown 		dump_atom_turbo_ratio_limits();
41050f7887c4SLen Brown 
4106fb5d4327SDasaratharaman Chandramouli 	if (has_knl_turbo_ratio_limit(family, model))
4107fb5d4327SDasaratharaman Chandramouli 		dump_knl_turbo_ratio_limits();
4108fb5d4327SDasaratharaman Chandramouli 
41096fb3143bSLen Brown 	if (has_config_tdp(family, model))
41106fb3143bSLen Brown 		dump_config_tdp();
41116fb3143bSLen Brown 
4112fcd17211SLen Brown 	dump_nhm_cst_cfg();
4113fcd17211SLen Brown }
4114fcd17211SLen Brown 
4115a5c6d65dSLen Brown static int read_sysfs_int(char *path)
4116a5c6d65dSLen Brown {
4117a5c6d65dSLen Brown 	FILE *input;
4118a5c6d65dSLen Brown 	int retval = -1;
4119a5c6d65dSLen Brown 
4120a5c6d65dSLen Brown 	input = fopen(path, "r");
4121a5c6d65dSLen Brown 	if (input == NULL) {
4122a5c6d65dSLen Brown 		if (debug)
4123a5c6d65dSLen Brown 			fprintf(outf, "NSFOD %s\n", path);
4124a5c6d65dSLen Brown 		return (-1);
4125a5c6d65dSLen Brown 	}
4126a5c6d65dSLen Brown 	if (fscanf(input, "%d", &retval) != 1)
4127a5c6d65dSLen Brown 		err(1, "%s: failed to read int from file", path);
4128a5c6d65dSLen Brown 	fclose(input);
4129a5c6d65dSLen Brown 
4130a5c6d65dSLen Brown 	return (retval);
4131a5c6d65dSLen Brown }
4132a5c6d65dSLen Brown 
4133abdcbdb2SLen Brown static void dump_sysfs_file(char *path)
4134abdcbdb2SLen Brown {
4135abdcbdb2SLen Brown 	FILE *input;
4136abdcbdb2SLen Brown 	char cpuidle_buf[64];
4137abdcbdb2SLen Brown 
4138abdcbdb2SLen Brown 	input = fopen(path, "r");
4139abdcbdb2SLen Brown 	if (input == NULL) {
4140abdcbdb2SLen Brown 		if (debug)
4141abdcbdb2SLen Brown 			fprintf(outf, "NSFOD %s\n", path);
4142abdcbdb2SLen Brown 		return;
4143abdcbdb2SLen Brown 	}
4144abdcbdb2SLen Brown 	if (!fgets(cpuidle_buf, sizeof(cpuidle_buf), input))
4145abdcbdb2SLen Brown 		err(1, "%s: failed to read file", path);
4146abdcbdb2SLen Brown 	fclose(input);
4147abdcbdb2SLen Brown 
4148abdcbdb2SLen Brown 	fprintf(outf, "%s: %s", strrchr(path, '/') + 1, cpuidle_buf);
4149abdcbdb2SLen Brown }
41501b439f01SLen Brown 
4151a5c6d65dSLen Brown static void intel_uncore_frequency_probe(void)
4152a5c6d65dSLen Brown {
4153a5c6d65dSLen Brown 	int i, j;
4154a5c6d65dSLen Brown 	char path[128];
4155a5c6d65dSLen Brown 
4156a5c6d65dSLen Brown 	if (!genuine_intel)
4157a5c6d65dSLen Brown 		return;
4158a5c6d65dSLen Brown 
4159a5c6d65dSLen Brown 	if (access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00", R_OK))
4160a5c6d65dSLen Brown 		return;
4161a5c6d65dSLen Brown 
4162a5c6d65dSLen Brown 	if (!access("/sys/devices/system/cpu/intel_uncore_frequency/package_00_die_00/current_freq_khz", R_OK))
4163a5c6d65dSLen Brown 		BIC_PRESENT(BIC_UNCORE_MHZ);
4164a5c6d65dSLen Brown 
4165a5c6d65dSLen Brown 	if (quiet)
4166a5c6d65dSLen Brown 		return;
4167a5c6d65dSLen Brown 
4168a5c6d65dSLen Brown 	for (i = 0; i < topo.num_packages; ++i) {
4169a5c6d65dSLen Brown 		for (j = 0; j < topo.num_die; ++j) {
4170a5c6d65dSLen Brown 			int k, l;
4171a5c6d65dSLen Brown 
4172a5c6d65dSLen Brown 			sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/min_freq_khz",
4173a5c6d65dSLen Brown 				i, j);
4174a5c6d65dSLen Brown 			k = read_sysfs_int(path);
4175a5c6d65dSLen Brown 			sprintf(path, "/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/max_freq_khz",
4176a5c6d65dSLen Brown 				i, j);
4177a5c6d65dSLen Brown 			l = read_sysfs_int(path);
4178a5c6d65dSLen Brown 			fprintf(outf, "Uncore Frequency pkg%d die%d: %d - %d MHz ", i, j, k / 1000, l / 1000);
4179a5c6d65dSLen Brown 
4180a5c6d65dSLen Brown 			sprintf(path,
4181a5c6d65dSLen Brown 				"/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_min_freq_khz",
4182a5c6d65dSLen Brown 				i, j);
4183a5c6d65dSLen Brown 			k = read_sysfs_int(path);
4184a5c6d65dSLen Brown 			sprintf(path,
4185a5c6d65dSLen Brown 				"/sys/devices/system/cpu/intel_uncore_frequency/package_0%d_die_0%d/initial_max_freq_khz",
4186a5c6d65dSLen Brown 				i, j);
4187a5c6d65dSLen Brown 			l = read_sysfs_int(path);
4188a5c6d65dSLen Brown 			fprintf(outf, "(%d - %d MHz)\n", k / 1000, l / 1000);
4189a5c6d65dSLen Brown 		}
4190a5c6d65dSLen Brown 	}
4191a5c6d65dSLen Brown }
4192a5c6d65dSLen Brown 
41931b439f01SLen Brown static void dump_sysfs_cstate_config(void)
419441618e63SLen Brown {
419541618e63SLen Brown 	char path[64];
419641618e63SLen Brown 	char name_buf[16];
419741618e63SLen Brown 	char desc[64];
419841618e63SLen Brown 	FILE *input;
419941618e63SLen Brown 	int state;
420041618e63SLen Brown 	char *sp;
420141618e63SLen Brown 
4202abdcbdb2SLen Brown 	if (access("/sys/devices/system/cpu/cpuidle", R_OK)) {
4203abdcbdb2SLen Brown 		fprintf(outf, "cpuidle not loaded\n");
4204abdcbdb2SLen Brown 		return;
4205abdcbdb2SLen Brown 	}
4206abdcbdb2SLen Brown 
4207abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_driver");
4208abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor");
4209abdcbdb2SLen Brown 	dump_sysfs_file("/sys/devices/system/cpu/cpuidle/current_governor_ro");
4210abdcbdb2SLen Brown 
421141618e63SLen Brown 	for (state = 0; state < 10; ++state) {
421241618e63SLen Brown 
42131b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
421441618e63SLen Brown 		input = fopen(path, "r");
421541618e63SLen Brown 		if (input == NULL)
421641618e63SLen Brown 			continue;
42178173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
42188173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
421941618e63SLen Brown 
422041618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
422141618e63SLen Brown 		sp = strchr(name_buf, '-');
422241618e63SLen Brown 		if (!sp)
422341618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
422441618e63SLen Brown 		*sp = '\0';
422541618e63SLen Brown 		fclose(input);
422641618e63SLen Brown 
4227fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
4228fecb3bc8SDavid Arcari 
42291b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/desc", base_cpu, state);
423041618e63SLen Brown 		input = fopen(path, "r");
423141618e63SLen Brown 		if (input == NULL)
423241618e63SLen Brown 			continue;
42338173c336SBen Hutchings 		if (!fgets(desc, sizeof(desc), input))
42348173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
423541618e63SLen Brown 
423641618e63SLen Brown 		fprintf(outf, "cpu%d: %s: %s", base_cpu, name_buf, desc);
423741618e63SLen Brown 		fclose(input);
423841618e63SLen Brown 	}
423941618e63SLen Brown }
42401b439f01SLen Brown 
42411b439f01SLen Brown static void dump_sysfs_pstate_config(void)
42427293fccdSLen Brown {
42437293fccdSLen Brown 	char path[64];
42447293fccdSLen Brown 	char driver_buf[64];
42457293fccdSLen Brown 	char governor_buf[64];
42467293fccdSLen Brown 	FILE *input;
42477293fccdSLen Brown 	int turbo;
42487293fccdSLen Brown 
42491b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_driver", base_cpu);
42507293fccdSLen Brown 	input = fopen(path, "r");
42517293fccdSLen Brown 	if (input == NULL) {
42520a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
42537293fccdSLen Brown 		return;
42547293fccdSLen Brown 	}
42558173c336SBen Hutchings 	if (!fgets(driver_buf, sizeof(driver_buf), input))
42568173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
42577293fccdSLen Brown 	fclose(input);
42587293fccdSLen Brown 
42591b439f01SLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", base_cpu);
42607293fccdSLen Brown 	input = fopen(path, "r");
42617293fccdSLen Brown 	if (input == NULL) {
42620a42d235SPrarit Bhargava 		fprintf(outf, "NSFOD %s\n", path);
42637293fccdSLen Brown 		return;
42647293fccdSLen Brown 	}
42658173c336SBen Hutchings 	if (!fgets(governor_buf, sizeof(governor_buf), input))
42668173c336SBen Hutchings 		err(1, "%s: failed to read file", path);
42677293fccdSLen Brown 	fclose(input);
42687293fccdSLen Brown 
42697293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq driver: %s", base_cpu, driver_buf);
42707293fccdSLen Brown 	fprintf(outf, "cpu%d: cpufreq governor: %s", base_cpu, governor_buf);
42717293fccdSLen Brown 
42727293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/cpufreq/boost");
42737293fccdSLen Brown 	input = fopen(path, "r");
42747293fccdSLen Brown 	if (input != NULL) {
42758173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
42768173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
42777293fccdSLen Brown 		fprintf(outf, "cpufreq boost: %d\n", turbo);
42787293fccdSLen Brown 		fclose(input);
42797293fccdSLen Brown 	}
42807293fccdSLen Brown 
42817293fccdSLen Brown 	sprintf(path, "/sys/devices/system/cpu/intel_pstate/no_turbo");
42827293fccdSLen Brown 	input = fopen(path, "r");
42837293fccdSLen Brown 	if (input != NULL) {
42848173c336SBen Hutchings 		if (fscanf(input, "%d", &turbo) != 1)
42858173c336SBen Hutchings 			err(1, "%s: failed to parse number from file", path);
42867293fccdSLen Brown 		fprintf(outf, "cpufreq intel_pstate no_turbo: %d\n", turbo);
42877293fccdSLen Brown 		fclose(input);
42887293fccdSLen Brown 	}
42897293fccdSLen Brown }
429041618e63SLen Brown 
4291889facbeSLen Brown /*
4292889facbeSLen Brown  * print_epb()
4293889facbeSLen Brown  * Decode the ENERGY_PERF_BIAS MSR
4294889facbeSLen Brown  */
4295889facbeSLen Brown int print_epb(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4296889facbeSLen Brown {
4297889facbeSLen Brown 	char *epb_string;
42986d6501d9SBorislav Petkov 	int cpu, epb;
4299889facbeSLen Brown 
43009878bf7aSLen Brown 	UNUSED(c);
43019878bf7aSLen Brown 	UNUSED(p);
43029878bf7aSLen Brown 
4303889facbeSLen Brown 	if (!has_epb)
4304889facbeSLen Brown 		return 0;
4305889facbeSLen Brown 
4306889facbeSLen Brown 	cpu = t->cpu_id;
4307889facbeSLen Brown 
4308889facbeSLen Brown 	/* EPB is per-package */
4309889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4310889facbeSLen Brown 		return 0;
4311889facbeSLen Brown 
4312889facbeSLen Brown 	if (cpu_migrate(cpu)) {
43133d7772eaSLen Brown 		fprintf(outf, "print_epb: Could not migrate to CPU %d\n", cpu);
4314889facbeSLen Brown 		return -1;
4315889facbeSLen Brown 	}
4316889facbeSLen Brown 
43176d6501d9SBorislav Petkov 	epb = get_epb(cpu);
43186d6501d9SBorislav Petkov 	if (epb < 0)
4319889facbeSLen Brown 		return 0;
4320889facbeSLen Brown 
43216d6501d9SBorislav Petkov 	switch (epb) {
4322889facbeSLen Brown 	case ENERGY_PERF_BIAS_PERFORMANCE:
4323889facbeSLen Brown 		epb_string = "performance";
4324889facbeSLen Brown 		break;
4325889facbeSLen Brown 	case ENERGY_PERF_BIAS_NORMAL:
4326889facbeSLen Brown 		epb_string = "balanced";
4327889facbeSLen Brown 		break;
4328889facbeSLen Brown 	case ENERGY_PERF_BIAS_POWERSAVE:
4329889facbeSLen Brown 		epb_string = "powersave";
4330889facbeSLen Brown 		break;
4331889facbeSLen Brown 	default:
4332889facbeSLen Brown 		epb_string = "custom";
4333889facbeSLen Brown 		break;
4334889facbeSLen Brown 	}
43356d6501d9SBorislav Petkov 	fprintf(outf, "cpu%d: EPB: %d (%s)\n", cpu, epb, epb_string);
4336889facbeSLen Brown 
4337889facbeSLen Brown 	return 0;
4338889facbeSLen Brown }
43391b439f01SLen Brown 
43407f5c258eSLen Brown /*
43417f5c258eSLen Brown  * print_hwp()
43427f5c258eSLen Brown  * Decode the MSR_HWP_CAPABILITIES
43437f5c258eSLen Brown  */
43447f5c258eSLen Brown int print_hwp(struct thread_data *t, struct core_data *c, struct pkg_data *p)
43457f5c258eSLen Brown {
43467f5c258eSLen Brown 	unsigned long long msr;
43477f5c258eSLen Brown 	int cpu;
43487f5c258eSLen Brown 
43499878bf7aSLen Brown 	UNUSED(c);
43509878bf7aSLen Brown 	UNUSED(p);
43519878bf7aSLen Brown 
43527f5c258eSLen Brown 	if (!has_hwp)
43537f5c258eSLen Brown 		return 0;
43547f5c258eSLen Brown 
43557f5c258eSLen Brown 	cpu = t->cpu_id;
43567f5c258eSLen Brown 
43577f5c258eSLen Brown 	/* MSR_HWP_CAPABILITIES is per-package */
43587f5c258eSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
43597f5c258eSLen Brown 		return 0;
43607f5c258eSLen Brown 
43617f5c258eSLen Brown 	if (cpu_migrate(cpu)) {
43623d7772eaSLen Brown 		fprintf(outf, "print_hwp: Could not migrate to CPU %d\n", cpu);
43637f5c258eSLen Brown 		return -1;
43647f5c258eSLen Brown 	}
43657f5c258eSLen Brown 
43667f5c258eSLen Brown 	if (get_msr(cpu, MSR_PM_ENABLE, &msr))
43677f5c258eSLen Brown 		return 0;
43687f5c258eSLen Brown 
43691b439f01SLen Brown 	fprintf(outf, "cpu%d: MSR_PM_ENABLE: 0x%08llx (%sHWP)\n", cpu, msr, (msr & (1 << 0)) ? "" : "No-");
43707f5c258eSLen Brown 
43717f5c258eSLen Brown 	/* MSR_PM_ENABLE[1] == 1 if HWP is enabled and MSRs visible */
43727f5c258eSLen Brown 	if ((msr & (1 << 0)) == 0)
43737f5c258eSLen Brown 		return 0;
43747f5c258eSLen Brown 
43757f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_CAPABILITIES, &msr))
43767f5c258eSLen Brown 		return 0;
43777f5c258eSLen Brown 
4378b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_CAPABILITIES: 0x%08llx "
43796dbd25a2SLen Brown 		"(high %d guar %d eff %d low %d)\n",
43807f5c258eSLen Brown 		cpu, msr,
43817f5c258eSLen Brown 		(unsigned int)HWP_HIGHEST_PERF(msr),
43827f5c258eSLen Brown 		(unsigned int)HWP_GUARANTEED_PERF(msr),
43831b439f01SLen Brown 		(unsigned int)HWP_MOSTEFFICIENT_PERF(msr), (unsigned int)HWP_LOWEST_PERF(msr));
43847f5c258eSLen Brown 
43857f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_REQUEST, &msr))
43867f5c258eSLen Brown 		return 0;
43877f5c258eSLen Brown 
4388b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_REQUEST: 0x%08llx "
43896dbd25a2SLen Brown 		"(min %d max %d des %d epp 0x%x window 0x%x pkg 0x%x)\n",
43907f5c258eSLen Brown 		cpu, msr,
43917f5c258eSLen Brown 		(unsigned int)(((msr) >> 0) & 0xff),
43927f5c258eSLen Brown 		(unsigned int)(((msr) >> 8) & 0xff),
43937f5c258eSLen Brown 		(unsigned int)(((msr) >> 16) & 0xff),
43947f5c258eSLen Brown 		(unsigned int)(((msr) >> 24) & 0xff),
43951b439f01SLen Brown 		(unsigned int)(((msr) >> 32) & 0xff3), (unsigned int)(((msr) >> 42) & 0x1));
43967f5c258eSLen Brown 
43977f5c258eSLen Brown 	if (has_hwp_pkg) {
43987f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_REQUEST_PKG, &msr))
43997f5c258eSLen Brown 			return 0;
44007f5c258eSLen Brown 
4401b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_REQUEST_PKG: 0x%08llx "
44026dbd25a2SLen Brown 			"(min %d max %d des %d epp 0x%x window 0x%x)\n",
44037f5c258eSLen Brown 			cpu, msr,
44047f5c258eSLen Brown 			(unsigned int)(((msr) >> 0) & 0xff),
44057f5c258eSLen Brown 			(unsigned int)(((msr) >> 8) & 0xff),
44067f5c258eSLen Brown 			(unsigned int)(((msr) >> 16) & 0xff),
44071b439f01SLen Brown 			(unsigned int)(((msr) >> 24) & 0xff), (unsigned int)(((msr) >> 32) & 0xff3));
44087f5c258eSLen Brown 	}
44097f5c258eSLen Brown 	if (has_hwp_notify) {
44107f5c258eSLen Brown 		if (get_msr(cpu, MSR_HWP_INTERRUPT, &msr))
44117f5c258eSLen Brown 			return 0;
44127f5c258eSLen Brown 
4413b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_HWP_INTERRUPT: 0x%08llx "
44147f5c258eSLen Brown 			"(%s_Guaranteed_Perf_Change, %s_Excursion_Min)\n",
44151b439f01SLen Brown 			cpu, msr, ((msr) & 0x1) ? "EN" : "Dis", ((msr) & 0x2) ? "EN" : "Dis");
44167f5c258eSLen Brown 	}
44177f5c258eSLen Brown 	if (get_msr(cpu, MSR_HWP_STATUS, &msr))
44187f5c258eSLen Brown 		return 0;
44197f5c258eSLen Brown 
4420b7d8c148SLen Brown 	fprintf(outf, "cpu%d: MSR_HWP_STATUS: 0x%08llx "
44217f5c258eSLen Brown 		"(%sGuaranteed_Perf_Change, %sExcursion_Min)\n",
44221b439f01SLen Brown 		cpu, msr, ((msr) & 0x1) ? "" : "No-", ((msr) & 0x2) ? "" : "No-");
4423889facbeSLen Brown 
4424889facbeSLen Brown 	return 0;
4425889facbeSLen Brown }
4426889facbeSLen Brown 
44273a9a941dSLen Brown /*
44283a9a941dSLen Brown  * print_perf_limit()
44293a9a941dSLen Brown  */
44303a9a941dSLen Brown int print_perf_limit(struct thread_data *t, struct core_data *c, struct pkg_data *p)
44313a9a941dSLen Brown {
44323a9a941dSLen Brown 	unsigned long long msr;
44333a9a941dSLen Brown 	int cpu;
44343a9a941dSLen Brown 
44359878bf7aSLen Brown 	UNUSED(c);
44369878bf7aSLen Brown 	UNUSED(p);
44379878bf7aSLen Brown 
44383a9a941dSLen Brown 	cpu = t->cpu_id;
44393a9a941dSLen Brown 
44403a9a941dSLen Brown 	/* per-package */
44413a9a941dSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
44423a9a941dSLen Brown 		return 0;
44433a9a941dSLen Brown 
44443a9a941dSLen Brown 	if (cpu_migrate(cpu)) {
44453d7772eaSLen Brown 		fprintf(outf, "print_perf_limit: Could not migrate to CPU %d\n", cpu);
44463a9a941dSLen Brown 		return -1;
44473a9a941dSLen Brown 	}
44483a9a941dSLen Brown 
44493a9a941dSLen Brown 	if (do_core_perf_limit_reasons) {
44503a9a941dSLen Brown 		get_msr(cpu, MSR_CORE_PERF_LIMIT_REASONS, &msr);
4451b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_CORE_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4452b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)",
4453e33cbe85SLen Brown 			(msr & 1 << 15) ? "bit15, " : "",
44543a9a941dSLen Brown 			(msr & 1 << 14) ? "bit14, " : "",
4455e33cbe85SLen Brown 			(msr & 1 << 13) ? "Transitions, " : "",
4456e33cbe85SLen Brown 			(msr & 1 << 12) ? "MultiCoreTurbo, " : "",
4457e33cbe85SLen Brown 			(msr & 1 << 11) ? "PkgPwrL2, " : "",
4458e33cbe85SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "",
4459e33cbe85SLen Brown 			(msr & 1 << 9) ? "CorePwr, " : "",
4460e33cbe85SLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
4461e33cbe85SLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
4462e33cbe85SLen Brown 			(msr & 1 << 5) ? "Auto-HWP, " : "",
4463e33cbe85SLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
4464e33cbe85SLen Brown 			(msr & 1 << 2) ? "bit2, " : "",
44651b439f01SLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "", (msr & 1 << 0) ? "PROCHOT, " : "");
4466b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s%s%s%s%s%s%s)\n",
4467e33cbe85SLen Brown 			(msr & 1 << 31) ? "bit31, " : "",
44683a9a941dSLen Brown 			(msr & 1 << 30) ? "bit30, " : "",
4469e33cbe85SLen Brown 			(msr & 1 << 29) ? "Transitions, " : "",
4470e33cbe85SLen Brown 			(msr & 1 << 28) ? "MultiCoreTurbo, " : "",
4471e33cbe85SLen Brown 			(msr & 1 << 27) ? "PkgPwrL2, " : "",
4472e33cbe85SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "",
4473e33cbe85SLen Brown 			(msr & 1 << 25) ? "CorePwr, " : "",
4474e33cbe85SLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
4475e33cbe85SLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
4476e33cbe85SLen Brown 			(msr & 1 << 21) ? "Auto-HWP, " : "",
4477e33cbe85SLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
4478e33cbe85SLen Brown 			(msr & 1 << 18) ? "bit18, " : "",
44791b439f01SLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "", (msr & 1 << 16) ? "PROCHOT, " : "");
44803a9a941dSLen Brown 
44813a9a941dSLen Brown 	}
44823a9a941dSLen Brown 	if (do_gfx_perf_limit_reasons) {
44833a9a941dSLen Brown 		get_msr(cpu, MSR_GFX_PERF_LIMIT_REASONS, &msr);
4484b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_GFX_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4485b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s%s%s)",
44863a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
44873a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
44883a9a941dSLen Brown 			(msr & 1 << 4) ? "Graphics, " : "",
44893a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
44903a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
44913a9a941dSLen Brown 			(msr & 1 << 9) ? "GFXPwr, " : "",
44921b439f01SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4493b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s%s%s)\n",
44943a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
44953a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
44963a9a941dSLen Brown 			(msr & 1 << 20) ? "Graphics, " : "",
44973a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
44983a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
44993a9a941dSLen Brown 			(msr & 1 << 25) ? "GFXPwr, " : "",
45001b439f01SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
45013a9a941dSLen Brown 	}
45023a9a941dSLen Brown 	if (do_ring_perf_limit_reasons) {
45033a9a941dSLen Brown 		get_msr(cpu, MSR_RING_PERF_LIMIT_REASONS, &msr);
4504b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_RING_PERF_LIMIT_REASONS, 0x%08llx", cpu, msr);
4505b7d8c148SLen Brown 		fprintf(outf, " (Active: %s%s%s%s%s%s)",
45063a9a941dSLen Brown 			(msr & 1 << 0) ? "PROCHOT, " : "",
45073a9a941dSLen Brown 			(msr & 1 << 1) ? "ThermStatus, " : "",
45083a9a941dSLen Brown 			(msr & 1 << 6) ? "VR-Therm, " : "",
45093a9a941dSLen Brown 			(msr & 1 << 8) ? "Amps, " : "",
45101b439f01SLen Brown 			(msr & 1 << 10) ? "PkgPwrL1, " : "", (msr & 1 << 11) ? "PkgPwrL2, " : "");
4511b7d8c148SLen Brown 		fprintf(outf, " (Logged: %s%s%s%s%s%s)\n",
45123a9a941dSLen Brown 			(msr & 1 << 16) ? "PROCHOT, " : "",
45133a9a941dSLen Brown 			(msr & 1 << 17) ? "ThermStatus, " : "",
45143a9a941dSLen Brown 			(msr & 1 << 22) ? "VR-Therm, " : "",
45153a9a941dSLen Brown 			(msr & 1 << 24) ? "Amps, " : "",
45161b439f01SLen Brown 			(msr & 1 << 26) ? "PkgPwrL1, " : "", (msr & 1 << 27) ? "PkgPwrL2, " : "");
45173a9a941dSLen Brown 	}
45183a9a941dSLen Brown 	return 0;
45193a9a941dSLen Brown }
45203a9a941dSLen Brown 
4521889facbeSLen Brown #define	RAPL_POWER_GRANULARITY	0x7FFF	/* 15 bit power granularity */
4522889facbeSLen Brown #define	RAPL_TIME_GRANULARITY	0x3F	/* 6 bit time granularity */
4523889facbeSLen Brown 
45249392bd98SCalvin Walton double get_tdp_intel(unsigned int model)
4525144b44b1SLen Brown {
4526144b44b1SLen Brown 	unsigned long long msr;
4527144b44b1SLen Brown 
4528144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO)
45297ce7d5deSPrarit Bhargava 		if (!get_msr(base_cpu, MSR_PKG_POWER_INFO, &msr))
4530144b44b1SLen Brown 			return ((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units;
4531144b44b1SLen Brown 
4532144b44b1SLen Brown 	switch (model) {
4533f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:
45345ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:
4535144b44b1SLen Brown 		return 30.0;
4536144b44b1SLen Brown 	default:
4537144b44b1SLen Brown 		return 135.0;
4538144b44b1SLen Brown 	}
4539144b44b1SLen Brown }
4540144b44b1SLen Brown 
45419392bd98SCalvin Walton double get_tdp_amd(unsigned int family)
45429392bd98SCalvin Walton {
45439878bf7aSLen Brown 	UNUSED(family);
45449878bf7aSLen Brown 
454533eb8225SKim Phillips 	/* This is the max stock TDP of HEDT/Server Fam17h+ chips */
454633eb8225SKim Phillips 	return 280.0;
45479392bd98SCalvin Walton }
45489392bd98SCalvin Walton 
454940ee8e3bSAndrey Semin /*
455040ee8e3bSAndrey Semin  * rapl_dram_energy_units_probe()
455140ee8e3bSAndrey Semin  * Energy units are either hard-coded, or come from RAPL Energy Unit MSR.
455240ee8e3bSAndrey Semin  */
45531b439f01SLen Brown static double rapl_dram_energy_units_probe(int model, double rapl_energy_units)
455440ee8e3bSAndrey Semin {
455540ee8e3bSAndrey Semin 	/* only called for genuine_intel, family 6 */
455640ee8e3bSAndrey Semin 
455740ee8e3bSAndrey Semin 	switch (model) {
4558869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
4559869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4560abdc75abSZhang Rui 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
4561869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
45626397b641SLen Brown 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
4563684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:	/* SPR */
456440ee8e3bSAndrey Semin 		return (rapl_dram_energy_units = 15.3 / 1000000);
456540ee8e3bSAndrey Semin 	default:
456640ee8e3bSAndrey Semin 		return (rapl_energy_units);
456740ee8e3bSAndrey Semin 	}
456840ee8e3bSAndrey Semin }
456940ee8e3bSAndrey Semin 
45709392bd98SCalvin Walton void rapl_probe_intel(unsigned int family, unsigned int model)
4571889facbeSLen Brown {
4572889facbeSLen Brown 	unsigned long long msr;
4573144b44b1SLen Brown 	unsigned int time_unit;
4574889facbeSLen Brown 	double tdp;
4575889facbeSLen Brown 
4576889facbeSLen Brown 	if (family != 6)
4577889facbeSLen Brown 		return;
4578889facbeSLen Brown 
4579889facbeSLen Brown 	switch (model) {
4580869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
4581869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:
4582c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
458377e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
45845e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
4585c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
45865e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
4587144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_GFX | RAPL_PKG_POWER_INFO;
4588812db3f7SLen Brown 		if (rapl_joules) {
4589812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4590812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4591812db3f7SLen Brown 			BIC_PRESENT(BIC_GFX_J);
4592812db3f7SLen Brown 		} else {
4593812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4594812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4595812db3f7SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
4596812db3f7SLen Brown 		}
4597889facbeSLen Brown 		break;
4598869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
4599f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
4600e4085d54SLen Brown 		do_rapl = RAPL_PKG | RAPL_PKG_POWER_INFO;
4601812db3f7SLen Brown 		if (rapl_joules)
4602812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4603812db3f7SLen Brown 		else
4604812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4605e4085d54SLen Brown 		break;
4606f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
46071b439f01SLen Brown 		do_rapl =
46081b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
46091b439f01SLen Brown 		    | RAPL_GFX | RAPL_PKG_POWER_INFO;
4610f6708400SChen Yu 		if (rapl_joules) {
4611f6708400SChen Yu 			BIC_PRESENT(BIC_Pkg_J);
4612f6708400SChen Yu 			BIC_PRESENT(BIC_Cor_J);
4613f6708400SChen Yu 			BIC_PRESENT(BIC_RAM_J);
4614f6708400SChen Yu 			BIC_PRESENT(BIC_GFX_J);
4615f6708400SChen Yu 		} else {
4616f6708400SChen Yu 			BIC_PRESENT(BIC_PkgWatt);
4617f6708400SChen Yu 			BIC_PRESENT(BIC_CorWatt);
4618f6708400SChen Yu 			BIC_PRESENT(BIC_RAMWatt);
4619f6708400SChen Yu 			BIC_PRESENT(BIC_GFXWatt);
4620f6708400SChen Yu 		}
4621f6708400SChen Yu 		break;
462220de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
462320de0dabSAntti Laakso 		do_rapl = RAPL_PKG | RAPL_PKG_PERF_STATUS | RAPL_PKG_POWER_INFO;
462420de0dabSAntti Laakso 		BIC_PRESENT(BIC_PKG__);
462520de0dabSAntti Laakso 		if (rapl_joules)
462620de0dabSAntti Laakso 			BIC_PRESENT(BIC_Pkg_J);
462720de0dabSAntti Laakso 		else
462820de0dabSAntti Laakso 			BIC_PRESENT(BIC_PkgWatt);
462920de0dabSAntti Laakso 		break;
4630af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
4631af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
46321b439f01SLen Brown 		do_rapl =
46331b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS
46341b439f01SLen Brown 		    | RAPL_GFX | RAPL_PKG_POWER_INFO;
4635812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4636812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4637812db3f7SLen Brown 		if (rapl_joules) {
4638812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4639812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4640812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
464181824921SLen Brown 			BIC_PRESENT(BIC_GFX_J);
4642812db3f7SLen Brown 		} else {
4643812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4644812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4645812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
464681824921SLen Brown 			BIC_PRESENT(BIC_GFXWatt);
4647812db3f7SLen Brown 		}
46480b2bb692SLen Brown 		break;
4649869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
4650869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
4651869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
465225368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
4653684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:	/* SPR */
4654869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
46551b439f01SLen Brown 		do_rapl =
46561b439f01SLen Brown 		    RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
46571b439f01SLen Brown 		    RAPL_PKG_POWER_INFO;
4658812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4659812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4660812db3f7SLen Brown 		if (rapl_joules) {
4661812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4662812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4663812db3f7SLen Brown 		} else {
4664812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4665812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4666812db3f7SLen Brown 		}
4667e6f9bb3cSLen Brown 		break;
4668869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
4669869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:
46701b439f01SLen Brown 		do_rapl =
46711b439f01SLen Brown 		    RAPL_PKG | RAPL_CORES | RAPL_CORE_POLICY | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_PKG_PERF_STATUS |
46721b439f01SLen Brown 		    RAPL_DRAM_PERF_STATUS | RAPL_PKG_POWER_INFO;
4673812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4674812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4675812db3f7SLen Brown 		if (rapl_joules) {
4676812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4677812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4678812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4679812db3f7SLen Brown 		} else {
4680812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4681812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4682812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4683812db3f7SLen Brown 		}
4684144b44b1SLen Brown 		break;
4685f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
46865ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
4687144b44b1SLen Brown 		do_rapl = RAPL_PKG | RAPL_CORES;
4688812db3f7SLen Brown 		if (rapl_joules) {
4689812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4690812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4691812db3f7SLen Brown 		} else {
4692812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4693812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4694812db3f7SLen Brown 		}
4695889facbeSLen Brown 		break;
46965ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
46971b439f01SLen Brown 		do_rapl =
46981b439f01SLen Brown 		    RAPL_PKG | RAPL_DRAM | RAPL_DRAM_POWER_INFO | RAPL_DRAM_PERF_STATUS | RAPL_PKG_PERF_STATUS |
46991b439f01SLen Brown 		    RAPL_PKG_POWER_INFO | RAPL_CORES_ENERGY_STATUS;
4700812db3f7SLen Brown 		BIC_PRESENT(BIC_PKG__);
4701812db3f7SLen Brown 		BIC_PRESENT(BIC_RAM__);
4702812db3f7SLen Brown 		if (rapl_joules) {
4703812db3f7SLen Brown 			BIC_PRESENT(BIC_Pkg_J);
4704812db3f7SLen Brown 			BIC_PRESENT(BIC_Cor_J);
4705812db3f7SLen Brown 			BIC_PRESENT(BIC_RAM_J);
4706812db3f7SLen Brown 		} else {
4707812db3f7SLen Brown 			BIC_PRESENT(BIC_PkgWatt);
4708812db3f7SLen Brown 			BIC_PRESENT(BIC_CorWatt);
4709812db3f7SLen Brown 			BIC_PRESENT(BIC_RAMWatt);
4710812db3f7SLen Brown 		}
47110f644909SJacob Pan 		break;
4712889facbeSLen Brown 	default:
4713889facbeSLen Brown 		return;
4714889facbeSLen Brown 	}
4715889facbeSLen Brown 
4716889facbeSLen Brown 	/* units on package 0, verify later other packages match */
47177ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_RAPL_POWER_UNIT, &msr))
4718889facbeSLen Brown 		return;
4719889facbeSLen Brown 
4720889facbeSLen Brown 	rapl_power_units = 1.0 / (1 << (msr & 0xF));
4721f2c4db1bSPeter Zijlstra 	if (model == INTEL_FAM6_ATOM_SILVERMONT)
4722144b44b1SLen Brown 		rapl_energy_units = 1.0 * (1 << (msr >> 8 & 0x1F)) / 1000000;
4723144b44b1SLen Brown 	else
4724889facbeSLen Brown 		rapl_energy_units = 1.0 / (1 << (msr >> 8 & 0x1F));
4725889facbeSLen Brown 
472640ee8e3bSAndrey Semin 	rapl_dram_energy_units = rapl_dram_energy_units_probe(model, rapl_energy_units);
472740ee8e3bSAndrey Semin 
4728144b44b1SLen Brown 	time_unit = msr >> 16 & 0xF;
4729144b44b1SLen Brown 	if (time_unit == 0)
4730144b44b1SLen Brown 		time_unit = 0xA;
4731889facbeSLen Brown 
4732144b44b1SLen Brown 	rapl_time_units = 1.0 / (1 << (time_unit));
4733144b44b1SLen Brown 
47349392bd98SCalvin Walton 	tdp = get_tdp_intel(model);
4735889facbeSLen Brown 
4736889facbeSLen Brown 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
473796e47158SLen Brown 	if (!quiet)
4738b7d8c148SLen Brown 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
47399392bd98SCalvin Walton }
4740889facbeSLen Brown 
47419392bd98SCalvin Walton void rapl_probe_amd(unsigned int family, unsigned int model)
47429392bd98SCalvin Walton {
47439392bd98SCalvin Walton 	unsigned long long msr;
47449392bd98SCalvin Walton 	unsigned int eax, ebx, ecx, edx;
47459392bd98SCalvin Walton 	unsigned int has_rapl = 0;
47469392bd98SCalvin Walton 	double tdp;
47479392bd98SCalvin Walton 
47489878bf7aSLen Brown 	UNUSED(model);
47499878bf7aSLen Brown 
47509392bd98SCalvin Walton 	if (max_extended_level >= 0x80000007) {
47519392bd98SCalvin Walton 		__cpuid(0x80000007, eax, ebx, ecx, edx);
475233eb8225SKim Phillips 		/* RAPL (Fam 17h+) */
47539392bd98SCalvin Walton 		has_rapl = edx & (1 << 14);
47549392bd98SCalvin Walton 	}
47559392bd98SCalvin Walton 
475633eb8225SKim Phillips 	if (!has_rapl || family < 0x17)
4757889facbeSLen Brown 		return;
47589392bd98SCalvin Walton 
47599392bd98SCalvin Walton 	do_rapl = RAPL_AMD_F17H | RAPL_PER_CORE_ENERGY;
47603316f99aSCalvin Walton 	if (rapl_joules) {
47613316f99aSCalvin Walton 		BIC_PRESENT(BIC_Pkg_J);
47629392bd98SCalvin Walton 		BIC_PRESENT(BIC_Cor_J);
47633316f99aSCalvin Walton 	} else {
47643316f99aSCalvin Walton 		BIC_PRESENT(BIC_PkgWatt);
47659392bd98SCalvin Walton 		BIC_PRESENT(BIC_CorWatt);
47663316f99aSCalvin Walton 	}
47679392bd98SCalvin Walton 
47689392bd98SCalvin Walton 	if (get_msr(base_cpu, MSR_RAPL_PWR_UNIT, &msr))
47699392bd98SCalvin Walton 		return;
47709392bd98SCalvin Walton 
47719392bd98SCalvin Walton 	rapl_time_units = ldexp(1.0, -(msr >> 16 & 0xf));
47729392bd98SCalvin Walton 	rapl_energy_units = ldexp(1.0, -(msr >> 8 & 0x1f));
47739392bd98SCalvin Walton 	rapl_power_units = ldexp(1.0, -(msr & 0xf));
47749392bd98SCalvin Walton 
47759cfa8e04SPu Wen 	tdp = get_tdp_amd(family);
47769392bd98SCalvin Walton 
47779392bd98SCalvin Walton 	rapl_joule_counter_range = 0xFFFFFFFF * rapl_energy_units / tdp;
47789392bd98SCalvin Walton 	if (!quiet)
47799392bd98SCalvin Walton 		fprintf(outf, "RAPL: %.0f sec. Joule Counter Range, at %.0f Watts\n", rapl_joule_counter_range, tdp);
47809392bd98SCalvin Walton }
47819392bd98SCalvin Walton 
47829392bd98SCalvin Walton /*
47839392bd98SCalvin Walton  * rapl_probe()
47849392bd98SCalvin Walton  *
47859392bd98SCalvin Walton  * sets do_rapl, rapl_power_units, rapl_energy_units, rapl_time_units
47869392bd98SCalvin Walton  */
47879392bd98SCalvin Walton void rapl_probe(unsigned int family, unsigned int model)
47889392bd98SCalvin Walton {
47899392bd98SCalvin Walton 	if (genuine_intel)
47909392bd98SCalvin Walton 		rapl_probe_intel(family, model);
4791c1c10cc7SPu Wen 	if (authentic_amd || hygon_genuine)
47929392bd98SCalvin Walton 		rapl_probe_amd(family, model);
4793889facbeSLen Brown }
4794889facbeSLen Brown 
47951b69317dSColin Ian King void perf_limit_reasons_probe(unsigned int family, unsigned int model)
47963a9a941dSLen Brown {
47973a9a941dSLen Brown 	if (!genuine_intel)
47983a9a941dSLen Brown 		return;
47993a9a941dSLen Brown 
48003a9a941dSLen Brown 	if (family != 6)
48013a9a941dSLen Brown 		return;
48023a9a941dSLen Brown 
48033a9a941dSLen Brown 	switch (model) {
4804c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
480577e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
48065e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
48073a9a941dSLen Brown 		do_gfx_perf_limit_reasons = 1;
48089878bf7aSLen Brown 		/* FALLTHRU */
4809869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSX */
48103a9a941dSLen Brown 		do_core_perf_limit_reasons = 1;
48113a9a941dSLen Brown 		do_ring_perf_limit_reasons = 1;
48123a9a941dSLen Brown 	default:
48133a9a941dSLen Brown 		return;
48143a9a941dSLen Brown 	}
48153a9a941dSLen Brown }
48163a9a941dSLen Brown 
4817ac980e13SArtem Bityutskiy void automatic_cstate_conversion_probe(unsigned int family, unsigned int model)
4818ac980e13SArtem Bityutskiy {
4819eade39b2SArtem Bityutskiy 	if (family != 6)
4820eade39b2SArtem Bityutskiy 		return;
4821eade39b2SArtem Bityutskiy 
4822eade39b2SArtem Bityutskiy 	switch (model) {
4823eade39b2SArtem Bityutskiy 	case INTEL_FAM6_BROADWELL_X:
4824eade39b2SArtem Bityutskiy 	case INTEL_FAM6_SKYLAKE_X:
4825eade39b2SArtem Bityutskiy 	case INTEL_FAM6_ICELAKE_X:
4826eade39b2SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:
4827ac980e13SArtem Bityutskiy 		has_automatic_cstate_conversion = 1;
4828ac980e13SArtem Bityutskiy 	}
4829eade39b2SArtem Bityutskiy }
4830ac980e13SArtem Bityutskiy 
4831aeb01e6dSChen Yu void prewake_cstate_probe(unsigned int family, unsigned int model)
4832aeb01e6dSChen Yu {
4833684e40e9SArtem Bityutskiy 	if (is_icx(family, model) || is_spr(family, model))
4834aeb01e6dSChen Yu 		dis_cstate_prewake = 1;
4835aeb01e6dSChen Yu }
4836aeb01e6dSChen Yu 
4837889facbeSLen Brown int print_thermal(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4838889facbeSLen Brown {
4839889facbeSLen Brown 	unsigned long long msr;
4840f4896fa5SLen Brown 	unsigned int dts, dts2;
4841889facbeSLen Brown 	int cpu;
4842889facbeSLen Brown 
48439878bf7aSLen Brown 	UNUSED(c);
48449878bf7aSLen Brown 	UNUSED(p);
48459878bf7aSLen Brown 
4846889facbeSLen Brown 	if (!(do_dts || do_ptm))
4847889facbeSLen Brown 		return 0;
4848889facbeSLen Brown 
4849889facbeSLen Brown 	cpu = t->cpu_id;
4850889facbeSLen Brown 
4851889facbeSLen Brown 	/* DTS is per-core, no need to print for each thread */
4852889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE))
4853889facbeSLen Brown 		return 0;
4854889facbeSLen Brown 
4855889facbeSLen Brown 	if (cpu_migrate(cpu)) {
48563d7772eaSLen Brown 		fprintf(outf, "print_thermal: Could not migrate to CPU %d\n", cpu);
4857889facbeSLen Brown 		return -1;
4858889facbeSLen Brown 	}
4859889facbeSLen Brown 
4860889facbeSLen Brown 	if (do_ptm && (t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE)) {
4861889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_STATUS, &msr))
4862889facbeSLen Brown 			return 0;
4863889facbeSLen Brown 
4864889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
48651b439f01SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_STATUS: 0x%08llx (%d C)\n", cpu, msr, tj_max - dts);
4866889facbeSLen Brown 
4867889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_PACKAGE_THERM_INTERRUPT, &msr))
4868889facbeSLen Brown 			return 0;
4869889facbeSLen Brown 
4870889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4871889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4872b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_PACKAGE_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
487355279aefSZhang Rui 			cpu, msr, tj_max - dts, tj_max - dts2);
4874889facbeSLen Brown 	}
4875889facbeSLen Brown 
4876f4896fa5SLen Brown 	if (do_dts && debug) {
4877889facbeSLen Brown 		unsigned int resolution;
4878889facbeSLen Brown 
4879889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_STATUS, &msr))
4880889facbeSLen Brown 			return 0;
4881889facbeSLen Brown 
4882889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4883889facbeSLen Brown 		resolution = (msr >> 27) & 0xF;
4884b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_STATUS: 0x%08llx (%d C +/- %d)\n",
488555279aefSZhang Rui 			cpu, msr, tj_max - dts, resolution);
4886889facbeSLen Brown 
4887889facbeSLen Brown 		if (get_msr(cpu, MSR_IA32_THERM_INTERRUPT, &msr))
4888889facbeSLen Brown 			return 0;
4889889facbeSLen Brown 
4890889facbeSLen Brown 		dts = (msr >> 16) & 0x7F;
4891889facbeSLen Brown 		dts2 = (msr >> 8) & 0x7F;
4892b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_THERM_INTERRUPT: 0x%08llx (%d C, %d C)\n",
489355279aefSZhang Rui 			cpu, msr, tj_max - dts, tj_max - dts2);
4894889facbeSLen Brown 	}
4895889facbeSLen Brown 
4896889facbeSLen Brown 	return 0;
4897889facbeSLen Brown }
4898889facbeSLen Brown 
4899889facbeSLen Brown void print_power_limit_msr(int cpu, unsigned long long msr, char *label)
4900889facbeSLen Brown {
49016b398625SSumeet Pawnikar 	fprintf(outf, "cpu%d: %s: %sabled (%0.3f Watts, %f sec, clamp %sabled)\n",
4902889facbeSLen Brown 		cpu, label,
4903889facbeSLen Brown 		((msr >> 15) & 1) ? "EN" : "DIS",
4904889facbeSLen Brown 		((msr >> 0) & 0x7FFF) * rapl_power_units,
4905889facbeSLen Brown 		(1.0 + (((msr >> 22) & 0x3) / 4.0)) * (1 << ((msr >> 17) & 0x1F)) * rapl_time_units,
4906889facbeSLen Brown 		(((msr >> 16) & 1) ? "EN" : "DIS"));
4907889facbeSLen Brown 
4908889facbeSLen Brown 	return;
4909889facbeSLen Brown }
4910889facbeSLen Brown 
4911889facbeSLen Brown int print_rapl(struct thread_data *t, struct core_data *c, struct pkg_data *p)
4912889facbeSLen Brown {
4913889facbeSLen Brown 	unsigned long long msr;
49149392bd98SCalvin Walton 	const char *msr_name;
4915889facbeSLen Brown 	int cpu;
4916889facbeSLen Brown 
49179878bf7aSLen Brown 	UNUSED(c);
49189878bf7aSLen Brown 	UNUSED(p);
49199878bf7aSLen Brown 
4920889facbeSLen Brown 	if (!do_rapl)
4921889facbeSLen Brown 		return 0;
4922889facbeSLen Brown 
4923889facbeSLen Brown 	/* RAPL counters are per package, so print only for 1st thread/package */
4924889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
4925889facbeSLen Brown 		return 0;
4926889facbeSLen Brown 
4927889facbeSLen Brown 	cpu = t->cpu_id;
4928889facbeSLen Brown 	if (cpu_migrate(cpu)) {
49293d7772eaSLen Brown 		fprintf(outf, "print_rapl: Could not migrate to CPU %d\n", cpu);
4930889facbeSLen Brown 		return -1;
4931889facbeSLen Brown 	}
4932889facbeSLen Brown 
49339392bd98SCalvin Walton 	if (do_rapl & RAPL_AMD_F17H) {
49349392bd98SCalvin Walton 		msr_name = "MSR_RAPL_PWR_UNIT";
49359392bd98SCalvin Walton 		if (get_msr(cpu, MSR_RAPL_PWR_UNIT, &msr))
49369392bd98SCalvin Walton 			return -1;
49379392bd98SCalvin Walton 	} else {
49389392bd98SCalvin Walton 		msr_name = "MSR_RAPL_POWER_UNIT";
4939889facbeSLen Brown 		if (get_msr(cpu, MSR_RAPL_POWER_UNIT, &msr))
4940889facbeSLen Brown 			return -1;
49419392bd98SCalvin Walton 	}
4942889facbeSLen Brown 
49439392bd98SCalvin Walton 	fprintf(outf, "cpu%d: %s: 0x%08llx (%f Watts, %f Joules, %f sec.)\n", cpu, msr_name, msr,
4944144b44b1SLen Brown 		rapl_power_units, rapl_energy_units, rapl_time_units);
494596e47158SLen Brown 
4946144b44b1SLen Brown 	if (do_rapl & RAPL_PKG_POWER_INFO) {
4947144b44b1SLen Brown 
4948889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_INFO, &msr))
4949889facbeSLen Brown 			return -5;
4950889facbeSLen Brown 
4951b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_INFO: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4952889facbeSLen Brown 			cpu, msr,
4953889facbeSLen Brown 			((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4954889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4955889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4956889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
4957889facbeSLen Brown 
4958144b44b1SLen Brown 	}
4959144b44b1SLen Brown 	if (do_rapl & RAPL_PKG) {
4960144b44b1SLen Brown 
4961889facbeSLen Brown 		if (get_msr(cpu, MSR_PKG_POWER_LIMIT, &msr))
4962889facbeSLen Brown 			return -9;
4963889facbeSLen Brown 
4964b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PKG_POWER_LIMIT: 0x%08llx (%slocked)\n",
4965889facbeSLen Brown 			cpu, msr, (msr >> 63) & 1 ? "" : "UN");
4966889facbeSLen Brown 
4967889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "PKG Limit #1");
49686b398625SSumeet Pawnikar 		fprintf(outf, "cpu%d: PKG Limit #2: %sabled (%0.3f Watts, %f* sec, clamp %sabled)\n",
4969889facbeSLen Brown 			cpu,
4970889facbeSLen Brown 			((msr >> 47) & 1) ? "EN" : "DIS",
4971889facbeSLen Brown 			((msr >> 32) & 0x7FFF) * rapl_power_units,
4972889facbeSLen Brown 			(1.0 + (((msr >> 54) & 0x3) / 4.0)) * (1 << ((msr >> 49) & 0x1F)) * rapl_time_units,
4973889facbeSLen Brown 			((msr >> 48) & 1) ? "EN" : "DIS");
4974f52ba931SSumeet Pawnikar 
4975f52ba931SSumeet Pawnikar 		if (get_msr(cpu, MSR_VR_CURRENT_CONFIG, &msr))
4976f52ba931SSumeet Pawnikar 			return -9;
4977f52ba931SSumeet Pawnikar 
4978f52ba931SSumeet Pawnikar 		fprintf(outf, "cpu%d: MSR_VR_CURRENT_CONFIG: 0x%08llx\n", cpu, msr);
4979f52ba931SSumeet Pawnikar 		fprintf(outf, "cpu%d: PKG Limit #4: %f Watts (%slocked)\n",
4980164d7a96SLen Brown 			cpu, ((msr >> 0) & 0x1FFF) * rapl_power_units, (msr >> 31) & 1 ? "" : "UN");
4981889facbeSLen Brown 	}
4982889facbeSLen Brown 
49830b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM_POWER_INFO) {
4984889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_INFO, &msr))
4985889facbeSLen Brown 			return -6;
4986889facbeSLen Brown 
4987b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_INFO,: 0x%08llx (%.0f W TDP, RAPL %.0f - %.0f W, %f sec.)\n",
4988889facbeSLen Brown 			cpu, msr,
4989889facbeSLen Brown 			((msr >> 0) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4990889facbeSLen Brown 			((msr >> 16) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4991889facbeSLen Brown 			((msr >> 32) & RAPL_POWER_GRANULARITY) * rapl_power_units,
4992889facbeSLen Brown 			((msr >> 48) & RAPL_TIME_GRANULARITY) * rapl_time_units);
49930b2bb692SLen Brown 	}
49940b2bb692SLen Brown 	if (do_rapl & RAPL_DRAM) {
4995889facbeSLen Brown 		if (get_msr(cpu, MSR_DRAM_POWER_LIMIT, &msr))
4996889facbeSLen Brown 			return -9;
4997b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_DRAM_POWER_LIMIT: 0x%08llx (%slocked)\n",
4998889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
4999889facbeSLen Brown 
5000889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "DRAM Limit");
5001889facbeSLen Brown 	}
5002144b44b1SLen Brown 	if (do_rapl & RAPL_CORE_POLICY) {
5003889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POLICY, &msr))
5004889facbeSLen Brown 			return -7;
5005889facbeSLen Brown 
5006b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POLICY: %lld\n", cpu, msr & 0xF);
5007144b44b1SLen Brown 	}
50089148494cSJacob Pan 	if (do_rapl & RAPL_CORES_POWER_LIMIT) {
5009889facbeSLen Brown 		if (get_msr(cpu, MSR_PP0_POWER_LIMIT, &msr))
5010889facbeSLen Brown 			return -9;
5011b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP0_POWER_LIMIT: 0x%08llx (%slocked)\n",
5012889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5013889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "Cores Limit");
5014889facbeSLen Brown 	}
5015889facbeSLen Brown 	if (do_rapl & RAPL_GFX) {
5016889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POLICY, &msr))
5017889facbeSLen Brown 			return -8;
5018889facbeSLen Brown 
5019b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POLICY: %lld\n", cpu, msr & 0xF);
5020889facbeSLen Brown 
5021889facbeSLen Brown 		if (get_msr(cpu, MSR_PP1_POWER_LIMIT, &msr))
5022889facbeSLen Brown 			return -9;
5023b7d8c148SLen Brown 		fprintf(outf, "cpu%d: MSR_PP1_POWER_LIMIT: 0x%08llx (%slocked)\n",
5024889facbeSLen Brown 			cpu, msr, (msr >> 31) & 1 ? "" : "UN");
5025889facbeSLen Brown 		print_power_limit_msr(cpu, msr, "GFX Limit");
5026889facbeSLen Brown 	}
5027889facbeSLen Brown 	return 0;
5028889facbeSLen Brown }
5029889facbeSLen Brown 
5030d7899447SLen Brown /*
5031d7899447SLen Brown  * SNB adds support for additional MSRs:
5032d7899447SLen Brown  *
5033d7899447SLen Brown  * MSR_PKG_C7_RESIDENCY            0x000003fa
5034d7899447SLen Brown  * MSR_CORE_C7_RESIDENCY           0x000003fe
5035d7899447SLen Brown  * MSR_PKG_C2_RESIDENCY            0x0000060d
5036d7899447SLen Brown  */
5037103a8feaSLen Brown 
5038d7899447SLen Brown int has_snb_msrs(unsigned int family, unsigned int model)
5039103a8feaSLen Brown {
5040103a8feaSLen Brown 	if (!genuine_intel)
5041103a8feaSLen Brown 		return 0;
5042103a8feaSLen Brown 
50439878bf7aSLen Brown 	if (family != 6)
50449878bf7aSLen Brown 		return 0;
50459878bf7aSLen Brown 
5046103a8feaSLen Brown 	switch (model) {
5047869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE:
5048869ce69eSLen Brown 	case INTEL_FAM6_SANDYBRIDGE_X:
5049869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE:	/* IVB */
5050869ce69eSLen Brown 	case INTEL_FAM6_IVYBRIDGE_X:	/* IVB Xeon */
5051c66f78a6SPeter Zijlstra 	case INTEL_FAM6_HASWELL:	/* HSW */
5052869ce69eSLen Brown 	case INTEL_FAM6_HASWELL_X:	/* HSW */
505377e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
50545e741407SPeter Zijlstra 	case INTEL_FAM6_HASWELL_G:	/* HSW */
5055c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
50565e741407SPeter Zijlstra 	case INTEL_FAM6_BROADWELL_G:	/* BDW */
5057869ce69eSLen Brown 	case INTEL_FAM6_BROADWELL_X:	/* BDX */
5058af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
5059af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
5060869ce69eSLen Brown 	case INTEL_FAM6_SKYLAKE_X:	/* SKX */
506125368d7cSChen Yu 	case INTEL_FAM6_ICELAKE_X:	/* ICX */
5062684e40e9SArtem Bityutskiy 	case INTEL_FAM6_SAPPHIRERAPIDS_X:	/* SPR */
5063869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
5064f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
50655ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
5066f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
506720de0dabSAntti Laakso 	case INTEL_FAM6_ATOM_TREMONT_D:	/* JVL */
5068103a8feaSLen Brown 		return 1;
5069103a8feaSLen Brown 	}
5070103a8feaSLen Brown 	return 0;
5071103a8feaSLen Brown }
5072103a8feaSLen Brown 
5073d7899447SLen Brown /*
5074570992fcSLen Brown  * HSW ULT added support for C8/C9/C10 MSRs:
5075d7899447SLen Brown  *
5076d7899447SLen Brown  * MSR_PKG_C8_RESIDENCY		0x00000630
5077d7899447SLen Brown  * MSR_PKG_C9_RESIDENCY		0x00000631
5078d7899447SLen Brown  * MSR_PKG_C10_RESIDENCY	0x00000632
50795a63426eSLen Brown  *
50805a63426eSLen Brown  * MSR_PKGC8_IRTL		0x00000633
50815a63426eSLen Brown  * MSR_PKGC9_IRTL		0x00000634
50825a63426eSLen Brown  * MSR_PKGC10_IRTL		0x00000635
50835a63426eSLen Brown  *
5084d7899447SLen Brown  */
5085570992fcSLen Brown int has_c8910_msrs(unsigned int family, unsigned int model)
5086ca58710fSKristen Carlson Accardi {
5087ca58710fSKristen Carlson Accardi 	if (!genuine_intel)
5088ca58710fSKristen Carlson Accardi 		return 0;
5089ca58710fSKristen Carlson Accardi 
50909878bf7aSLen Brown 	if (family != 6)
50919878bf7aSLen Brown 		return 0;
50929878bf7aSLen Brown 
5093ca58710fSKristen Carlson Accardi 	switch (model) {
509477e5517cSIngo Molnar 	case INTEL_FAM6_HASWELL_L:	/* HSW */
5095c66f78a6SPeter Zijlstra 	case INTEL_FAM6_BROADWELL:	/* BDW */
5096af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
5097af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
5098869ce69eSLen Brown 	case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
5099f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5100f6708400SChen Yu 	case INTEL_FAM6_ATOM_TREMONT:	/* EHL */
5101ca58710fSKristen Carlson Accardi 		return 1;
5102ca58710fSKristen Carlson Accardi 	}
5103ca58710fSKristen Carlson Accardi 	return 0;
5104ca58710fSKristen Carlson Accardi }
5105ca58710fSKristen Carlson Accardi 
51060b2bb692SLen Brown /*
51070b2bb692SLen Brown  * SKL adds support for additional MSRS:
51080b2bb692SLen Brown  *
51090b2bb692SLen Brown  * MSR_PKG_WEIGHTED_CORE_C0_RES    0x00000658
51100b2bb692SLen Brown  * MSR_PKG_ANY_CORE_C0_RES         0x00000659
51110b2bb692SLen Brown  * MSR_PKG_ANY_GFXE_C0_RES         0x0000065A
51120b2bb692SLen Brown  * MSR_PKG_BOTH_CORE_GFXE_C0_RES   0x0000065B
51130b2bb692SLen Brown  */
51140b2bb692SLen Brown int has_skl_msrs(unsigned int family, unsigned int model)
51150b2bb692SLen Brown {
51160b2bb692SLen Brown 	if (!genuine_intel)
51170b2bb692SLen Brown 		return 0;
51180b2bb692SLen Brown 
51199878bf7aSLen Brown 	if (family != 6)
51209878bf7aSLen Brown 		return 0;
51219878bf7aSLen Brown 
51220b2bb692SLen Brown 	switch (model) {
5123af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:	/* SKL */
5124af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
51250b2bb692SLen Brown 		return 1;
51260b2bb692SLen Brown 	}
51270b2bb692SLen Brown 	return 0;
51280b2bb692SLen Brown }
51290b2bb692SLen Brown 
5130144b44b1SLen Brown int is_slm(unsigned int family, unsigned int model)
5131144b44b1SLen Brown {
5132144b44b1SLen Brown 	if (!genuine_intel)
5133144b44b1SLen Brown 		return 0;
51349878bf7aSLen Brown 
51359878bf7aSLen Brown 	if (family != 6)
51369878bf7aSLen Brown 		return 0;
51379878bf7aSLen Brown 
5138144b44b1SLen Brown 	switch (model) {
5139f2c4db1bSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT:	/* BYT */
51405ebb34edSPeter Zijlstra 	case INTEL_FAM6_ATOM_SILVERMONT_D:	/* AVN */
5141144b44b1SLen Brown 		return 1;
5142144b44b1SLen Brown 	}
5143144b44b1SLen Brown 	return 0;
5144144b44b1SLen Brown }
5145144b44b1SLen Brown 
5146fb5d4327SDasaratharaman Chandramouli int is_knl(unsigned int family, unsigned int model)
5147fb5d4327SDasaratharaman Chandramouli {
5148fb5d4327SDasaratharaman Chandramouli 	if (!genuine_intel)
5149fb5d4327SDasaratharaman Chandramouli 		return 0;
51509878bf7aSLen Brown 
51519878bf7aSLen Brown 	if (family != 6)
51529878bf7aSLen Brown 		return 0;
51539878bf7aSLen Brown 
5154fb5d4327SDasaratharaman Chandramouli 	switch (model) {
5155869ce69eSLen Brown 	case INTEL_FAM6_XEON_PHI_KNL:	/* KNL */
5156fb5d4327SDasaratharaman Chandramouli 		return 1;
5157fb5d4327SDasaratharaman Chandramouli 	}
5158fb5d4327SDasaratharaman Chandramouli 	return 0;
5159fb5d4327SDasaratharaman Chandramouli }
5160fb5d4327SDasaratharaman Chandramouli 
5161997e5395SSrinivas Pandruvada int is_cnl(unsigned int family, unsigned int model)
5162997e5395SSrinivas Pandruvada {
5163997e5395SSrinivas Pandruvada 	if (!genuine_intel)
5164997e5395SSrinivas Pandruvada 		return 0;
5165997e5395SSrinivas Pandruvada 
51669878bf7aSLen Brown 	if (family != 6)
51679878bf7aSLen Brown 		return 0;
51689878bf7aSLen Brown 
5169997e5395SSrinivas Pandruvada 	switch (model) {
5170af239c44SPeter Zijlstra 	case INTEL_FAM6_CANNONLAKE_L:	/* CNL */
5171997e5395SSrinivas Pandruvada 		return 1;
5172997e5395SSrinivas Pandruvada 	}
5173997e5395SSrinivas Pandruvada 
5174997e5395SSrinivas Pandruvada 	return 0;
5175997e5395SSrinivas Pandruvada }
5176997e5395SSrinivas Pandruvada 
5177b2b34dfeSHubert Chrzaniuk unsigned int get_aperf_mperf_multiplier(unsigned int family, unsigned int model)
5178b2b34dfeSHubert Chrzaniuk {
5179b2b34dfeSHubert Chrzaniuk 	if (is_knl(family, model))
5180b2b34dfeSHubert Chrzaniuk 		return 1024;
5181b2b34dfeSHubert Chrzaniuk 	return 1;
5182b2b34dfeSHubert Chrzaniuk }
5183b2b34dfeSHubert Chrzaniuk 
5184144b44b1SLen Brown #define SLM_BCLK_FREQS 5
5185144b44b1SLen Brown double slm_freq_table[SLM_BCLK_FREQS] = { 83.3, 100.0, 133.3, 116.7, 80.0 };
5186144b44b1SLen Brown 
5187144b44b1SLen Brown double slm_bclk(void)
5188144b44b1SLen Brown {
5189144b44b1SLen Brown 	unsigned long long msr = 3;
5190144b44b1SLen Brown 	unsigned int i;
5191144b44b1SLen Brown 	double freq;
5192144b44b1SLen Brown 
51937ce7d5deSPrarit Bhargava 	if (get_msr(base_cpu, MSR_FSB_FREQ, &msr))
5194b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: unknown\n");
5195144b44b1SLen Brown 
5196144b44b1SLen Brown 	i = msr & 0xf;
5197144b44b1SLen Brown 	if (i >= SLM_BCLK_FREQS) {
5198b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK[%d] invalid\n", i);
51990a91e551SColin Ian King 		i = 3;
5200144b44b1SLen Brown 	}
5201144b44b1SLen Brown 	freq = slm_freq_table[i];
5202144b44b1SLen Brown 
520396e47158SLen Brown 	if (!quiet)
5204b7d8c148SLen Brown 		fprintf(outf, "SLM BCLK: %.1f Mhz\n", freq);
5205144b44b1SLen Brown 
5206144b44b1SLen Brown 	return freq;
5207144b44b1SLen Brown }
5208144b44b1SLen Brown 
5209103a8feaSLen Brown double discover_bclk(unsigned int family, unsigned int model)
5210103a8feaSLen Brown {
5211121b48bbSChrzaniuk, Hubert 	if (has_snb_msrs(family, model) || is_knl(family, model))
5212103a8feaSLen Brown 		return 100.00;
5213144b44b1SLen Brown 	else if (is_slm(family, model))
5214144b44b1SLen Brown 		return slm_bclk();
5215103a8feaSLen Brown 	else
5216103a8feaSLen Brown 		return 133.33;
5217103a8feaSLen Brown }
5218103a8feaSLen Brown 
52197ab5ff49SZhang Rui int get_cpu_type(struct thread_data *t, struct core_data *c, struct pkg_data *p)
52207ab5ff49SZhang Rui {
52217ab5ff49SZhang Rui 	unsigned int eax, ebx, ecx, edx;
52227ab5ff49SZhang Rui 
52239878bf7aSLen Brown 	UNUSED(c);
52249878bf7aSLen Brown 	UNUSED(p);
52259878bf7aSLen Brown 
52267ab5ff49SZhang Rui 	if (!genuine_intel)
52277ab5ff49SZhang Rui 		return 0;
52287ab5ff49SZhang Rui 
52297ab5ff49SZhang Rui 	if (cpu_migrate(t->cpu_id)) {
52307ab5ff49SZhang Rui 		fprintf(outf, "Could not migrate to CPU %d\n", t->cpu_id);
52317ab5ff49SZhang Rui 		return -1;
52327ab5ff49SZhang Rui 	}
52337ab5ff49SZhang Rui 
52347ab5ff49SZhang Rui 	if (max_level < 0x1a)
52357ab5ff49SZhang Rui 		return 0;
52367ab5ff49SZhang Rui 
52377ab5ff49SZhang Rui 	__cpuid(0x1a, eax, ebx, ecx, edx);
52387ab5ff49SZhang Rui 	eax = (eax >> 24) & 0xFF;
52397ab5ff49SZhang Rui 	if (eax == 0x20)
52407ab5ff49SZhang Rui 		t->is_atom = true;
52417ab5ff49SZhang Rui 	return 0;
52427ab5ff49SZhang Rui }
52437ab5ff49SZhang Rui 
5244889facbeSLen Brown /*
5245889facbeSLen Brown  * MSR_IA32_TEMPERATURE_TARGET indicates the temperature where
5246889facbeSLen Brown  * the Thermal Control Circuit (TCC) activates.
5247889facbeSLen Brown  * This is usually equal to tjMax.
5248889facbeSLen Brown  *
5249889facbeSLen Brown  * Older processors do not have this MSR, so there we guess,
5250889facbeSLen Brown  * but also allow cmdline over-ride with -T.
5251889facbeSLen Brown  *
5252889facbeSLen Brown  * Several MSR temperature values are in units of degrees-C
5253889facbeSLen Brown  * below this value, including the Digital Thermal Sensor (DTS),
5254889facbeSLen Brown  * Package Thermal Management Sensor (PTM), and thermal event thresholds.
5255889facbeSLen Brown  */
52566ff7cb37SLen Brown int set_temperature_target(struct thread_data *t, struct core_data *c, struct pkg_data *p)
52576ff7cb37SLen Brown {
5258b2b94be7SLen Brown 	unsigned long long msr;
525955279aefSZhang Rui 	unsigned int tcc_default, tcc_offset;
5260b2b94be7SLen Brown 	int cpu;
5261b2b94be7SLen Brown 
52629878bf7aSLen Brown 	UNUSED(c);
52639878bf7aSLen Brown 	UNUSED(p);
52649878bf7aSLen Brown 
526555279aefSZhang Rui 	/* tj_max is used only for dts or ptm */
5266889facbeSLen Brown 	if (!(do_dts || do_ptm))
5267889facbeSLen Brown 		return 0;
5268889facbeSLen Brown 
5269889facbeSLen Brown 	/* this is a per-package concept */
5270889facbeSLen Brown 	if (!(t->flags & CPU_IS_FIRST_THREAD_IN_CORE) || !(t->flags & CPU_IS_FIRST_CORE_IN_PACKAGE))
5271889facbeSLen Brown 		return 0;
5272889facbeSLen Brown 
5273b2b94be7SLen Brown 	cpu = t->cpu_id;
5274b2b94be7SLen Brown 	if (cpu_migrate(cpu)) {
5275b2b94be7SLen Brown 		fprintf(outf, "Could not migrate to CPU %d\n", cpu);
5276b2b94be7SLen Brown 		return -1;
5277b2b94be7SLen Brown 	}
5278b2b94be7SLen Brown 
527955279aefSZhang Rui 	if (tj_max_override != 0) {
528055279aefSZhang Rui 		tj_max = tj_max_override;
52811b439f01SLen Brown 		fprintf(outf, "cpu%d: Using cmdline TCC Target (%d C)\n", cpu, tj_max);
5282889facbeSLen Brown 		return 0;
5283889facbeSLen Brown 	}
5284889facbeSLen Brown 
5285b2b94be7SLen Brown 	/* Temperature Target MSR is Nehalem and newer only */
5286b2b94be7SLen Brown 	if (!do_nhm_platform_info)
5287b2b94be7SLen Brown 		goto guess;
5288b2b94be7SLen Brown 
5289b2b94be7SLen Brown 	if (get_msr(base_cpu, MSR_IA32_TEMPERATURE_TARGET, &msr))
5290b2b94be7SLen Brown 		goto guess;
5291b2b94be7SLen Brown 
529255279aefSZhang Rui 	tcc_default = (msr >> 16) & 0xFF;
5293b2b94be7SLen Brown 
52940b9a0b9bSZhang Rui 	if (!quiet) {
52950b9a0b9bSZhang Rui 		switch (tcc_offset_bits) {
52960b9a0b9bSZhang Rui 		case 4:
52970b9a0b9bSZhang Rui 			tcc_offset = (msr >> 24) & 0xF;
52980b9a0b9bSZhang Rui 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
529955279aefSZhang Rui 				cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
53000b9a0b9bSZhang Rui 			break;
53010b9a0b9bSZhang Rui 		case 6:
53020b9a0b9bSZhang Rui 			tcc_offset = (msr >> 24) & 0x3F;
53030b9a0b9bSZhang Rui 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C) (%d default - %d offset)\n",
530455279aefSZhang Rui 				cpu, msr, tcc_default - tcc_offset, tcc_default, tcc_offset);
53050b9a0b9bSZhang Rui 			break;
53060b9a0b9bSZhang Rui 		default:
53071b439f01SLen Brown 			fprintf(outf, "cpu%d: MSR_IA32_TEMPERATURE_TARGET: 0x%08llx (%d C)\n", cpu, msr, tcc_default);
53080b9a0b9bSZhang Rui 			break;
53090b9a0b9bSZhang Rui 		}
53100b9a0b9bSZhang Rui 	}
5311b2b94be7SLen Brown 
531255279aefSZhang Rui 	if (!tcc_default)
5313b2b94be7SLen Brown 		goto guess;
5314b2b94be7SLen Brown 
531555279aefSZhang Rui 	tj_max = tcc_default;
5316b2b94be7SLen Brown 
5317889facbeSLen Brown 	return 0;
5318889facbeSLen Brown 
5319b2b94be7SLen Brown guess:
532055279aefSZhang Rui 	tj_max = TJMAX_DEFAULT;
53211b439f01SLen Brown 	fprintf(outf, "cpu%d: Guessing tjMax %d C, Please use -T to specify\n", cpu, tj_max);
5322889facbeSLen Brown 
5323889facbeSLen Brown 	return 0;
5324889facbeSLen Brown }
532569807a63SLen Brown 
5326aa8d8cc7SLen Brown void decode_feature_control_msr(void)
5327aa8d8cc7SLen Brown {
5328aa8d8cc7SLen Brown 	unsigned long long msr;
5329aa8d8cc7SLen Brown 
5330f6505c88SSean Christopherson 	if (!get_msr(base_cpu, MSR_IA32_FEAT_CTL, &msr))
5331aa8d8cc7SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_FEATURE_CONTROL: 0x%08llx (%sLocked %s)\n",
53321b439f01SLen Brown 			base_cpu, msr, msr & FEAT_CTL_LOCKED ? "" : "UN-", msr & (1 << 18) ? "SGX" : "");
5333aa8d8cc7SLen Brown }
5334aa8d8cc7SLen Brown 
533569807a63SLen Brown void decode_misc_enable_msr(void)
533669807a63SLen Brown {
533769807a63SLen Brown 	unsigned long long msr;
533869807a63SLen Brown 
5339f26b1519SLen Brown 	if (!genuine_intel)
5340f26b1519SLen Brown 		return;
5341f26b1519SLen Brown 
534269807a63SLen Brown 	if (!get_msr(base_cpu, MSR_IA32_MISC_ENABLE, &msr))
5343e6512624SLen Brown 		fprintf(outf, "cpu%d: MSR_IA32_MISC_ENABLE: 0x%08llx (%sTCC %sEIST %sMWAIT %sPREFETCH %sTURBO)\n",
534469807a63SLen Brown 			base_cpu, msr,
5345e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TM1 ? "" : "No-",
5346e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_ENHANCED_SPEEDSTEP ? "" : "No-",
5347fd3933caSLen Brown 			msr & MSR_IA32_MISC_ENABLE_MWAIT ? "" : "No-",
5348e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_PREFETCH_DISABLE ? "No-" : "",
5349e6512624SLen Brown 			msr & MSR_IA32_MISC_ENABLE_TURBO_DISABLE ? "No-" : "");
535069807a63SLen Brown }
535169807a63SLen Brown 
535233148d67SLen Brown void decode_misc_feature_control(void)
535333148d67SLen Brown {
535433148d67SLen Brown 	unsigned long long msr;
535533148d67SLen Brown 
535633148d67SLen Brown 	if (!has_misc_feature_control)
535733148d67SLen Brown 		return;
535833148d67SLen Brown 
535933148d67SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_FEATURE_CONTROL, &msr))
53601b439f01SLen Brown 		fprintf(outf,
53611b439f01SLen Brown 			"cpu%d: MSR_MISC_FEATURE_CONTROL: 0x%08llx (%sL2-Prefetch %sL2-Prefetch-pair %sL1-Prefetch %sL1-IP-Prefetch)\n",
53621b439f01SLen Brown 			base_cpu, msr, msr & (0 << 0) ? "No-" : "", msr & (1 << 0) ? "No-" : "",
53631b439f01SLen Brown 			msr & (2 << 0) ? "No-" : "", msr & (3 << 0) ? "No-" : "");
536433148d67SLen Brown }
53651b439f01SLen Brown 
5366f0057310SLen Brown /*
5367f0057310SLen Brown  * Decode MSR_MISC_PWR_MGMT
5368f0057310SLen Brown  *
5369f0057310SLen Brown  * Decode the bits according to the Nehalem documentation
5370f0057310SLen Brown  * bit[0] seems to continue to have same meaning going forward
5371f0057310SLen Brown  * bit[1] less so...
5372f0057310SLen Brown  */
5373f0057310SLen Brown void decode_misc_pwr_mgmt_msr(void)
5374f0057310SLen Brown {
5375f0057310SLen Brown 	unsigned long long msr;
5376f0057310SLen Brown 
5377f0057310SLen Brown 	if (!do_nhm_platform_info)
5378f0057310SLen Brown 		return;
5379f0057310SLen Brown 
5380cf4cbe53SLen Brown 	if (no_MSR_MISC_PWR_MGMT)
5381cf4cbe53SLen Brown 		return;
5382cf4cbe53SLen Brown 
5383f0057310SLen Brown 	if (!get_msr(base_cpu, MSR_MISC_PWR_MGMT, &msr))
5384ddadb8adSSrinivas Pandruvada 		fprintf(outf, "cpu%d: MSR_MISC_PWR_MGMT: 0x%08llx (%sable-EIST_Coordination %sable-EPB %sable-OOB)\n",
5385f0057310SLen Brown 			base_cpu, msr,
53861b439f01SLen Brown 			msr & (1 << 0) ? "DIS" : "EN", msr & (1 << 1) ? "EN" : "DIS", msr & (1 << 8) ? "EN" : "DIS");
5387f0057310SLen Brown }
53881b439f01SLen Brown 
538971616c8eSLen Brown /*
539071616c8eSLen Brown  * Decode MSR_CC6_DEMOTION_POLICY_CONFIG, MSR_MC6_DEMOTION_POLICY_CONFIG
539171616c8eSLen Brown  *
539271616c8eSLen Brown  * This MSRs are present on Silvermont processors,
539371616c8eSLen Brown  * Intel Atom processor E3000 series (Baytrail), and friends.
539471616c8eSLen Brown  */
539571616c8eSLen Brown void decode_c6_demotion_policy_msr(void)
539671616c8eSLen Brown {
539771616c8eSLen Brown 	unsigned long long msr;
539871616c8eSLen Brown 
539971616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_CC6_DEMOTION_POLICY_CONFIG, &msr))
540071616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_CC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-CC6-Demotion)\n",
540171616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
540271616c8eSLen Brown 
540371616c8eSLen Brown 	if (!get_msr(base_cpu, MSR_MC6_DEMOTION_POLICY_CONFIG, &msr))
540471616c8eSLen Brown 		fprintf(outf, "cpu%d: MSR_MC6_DEMOTION_POLICY_CONFIG: 0x%08llx (%sable-MC6-Demotion)\n",
540571616c8eSLen Brown 			base_cpu, msr, msr & (1 << 0) ? "EN" : "DIS");
540671616c8eSLen Brown }
54077f5c258eSLen Brown 
5408f5a4c76aSLen Brown /*
5409f5a4c76aSLen Brown  * When models are the same, for the purpose of turbostat, reuse
5410f5a4c76aSLen Brown  */
5411f5a4c76aSLen Brown unsigned int intel_model_duplicates(unsigned int model)
5412f5a4c76aSLen Brown {
5413f5a4c76aSLen Brown 
5414f5a4c76aSLen Brown 	switch (model) {
5415f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EP:	/* Core i7, Xeon 5500 series - Bloomfield, Gainstown NHM-EP */
5416f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM:	/* Core i7 and i5 Processor - Clarksfield, Lynnfield, Jasper Forest */
5417f5a4c76aSLen Brown 	case 0x1F:		/* Core i7 and i5 Processor - Nehalem */
5418f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE:	/* Westmere Client - Clarkdale, Arrandale */
5419f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EP:	/* Westmere EP - Gulftown */
5420f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM;
5421f5a4c76aSLen Brown 
5422f5a4c76aSLen Brown 	case INTEL_FAM6_NEHALEM_EX:	/* Nehalem-EX Xeon - Beckton */
5423f5a4c76aSLen Brown 	case INTEL_FAM6_WESTMERE_EX:	/* Westmere-EX Xeon - Eagleton */
5424f5a4c76aSLen Brown 		return INTEL_FAM6_NEHALEM_EX;
5425f5a4c76aSLen Brown 
5426f5a4c76aSLen Brown 	case INTEL_FAM6_XEON_PHI_KNM:
5427f5a4c76aSLen Brown 		return INTEL_FAM6_XEON_PHI_KNL;
5428f5a4c76aSLen Brown 
5429f5a4c76aSLen Brown 	case INTEL_FAM6_BROADWELL_X:
54305ebb34edSPeter Zijlstra 	case INTEL_FAM6_BROADWELL_D:	/* BDX-DE */
5431f5a4c76aSLen Brown 		return INTEL_FAM6_BROADWELL_X;
5432f5a4c76aSLen Brown 
5433af239c44SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE_L:
5434c66f78a6SPeter Zijlstra 	case INTEL_FAM6_SKYLAKE:
5435af239c44SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE_L:
5436c66f78a6SPeter Zijlstra 	case INTEL_FAM6_KABYLAKE:
5437081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE_L:
5438081c5432SChen Yu 	case INTEL_FAM6_COMETLAKE:
5439af239c44SPeter Zijlstra 		return INTEL_FAM6_SKYLAKE_L;
5440937807d3SLen Brown 
5441af239c44SPeter Zijlstra 	case INTEL_FAM6_ICELAKE_L:
5442d93ea567SRajneesh Bhardwaj 	case INTEL_FAM6_ICELAKE_NNPI:
54434bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE_L:
54444bf7132aSChen Yu 	case INTEL_FAM6_TIGERLAKE:
5445e7af1ed3SLen Brown 	case INTEL_FAM6_ROCKETLAKE:
5446e7af1ed3SLen Brown 	case INTEL_FAM6_LAKEFIELD:
5447e7af1ed3SLen Brown 	case INTEL_FAM6_ALDERLAKE:
54485683460bSChen Yu 	case INTEL_FAM6_ALDERLAKE_L:
54491c1313b5SZhang Rui 	case INTEL_FAM6_ALDERLAKE_N:
5450fb5e29dfSZhang Rui 	case INTEL_FAM6_RAPTORLAKE:
54516f9cf553SGeorge D Sworo 	case INTEL_FAM6_RAPTORLAKE_P:
5452af239c44SPeter Zijlstra 		return INTEL_FAM6_CANNONLAKE_L;
5453b62b3184SZhang Rui 
5454d7814c30SChen Yu 	case INTEL_FAM6_ATOM_TREMONT_L:
5455d7814c30SChen Yu 		return INTEL_FAM6_ATOM_TREMONT;
5456d7814c30SChen Yu 
54576c5c6560SChen Yu 	case INTEL_FAM6_ICELAKE_D:
545825368d7cSChen Yu 		return INTEL_FAM6_ICELAKE_X;
5459f5a4c76aSLen Brown 	}
5460f5a4c76aSLen Brown 	return model;
5461f5a4c76aSLen Brown }
5462d76bb7a0SLen Brown 
5463d76bb7a0SLen Brown void print_dev_latency(void)
5464d76bb7a0SLen Brown {
5465d76bb7a0SLen Brown 	char *path = "/dev/cpu_dma_latency";
5466d76bb7a0SLen Brown 	int fd;
5467d76bb7a0SLen Brown 	int value;
5468d76bb7a0SLen Brown 	int retval;
5469d76bb7a0SLen Brown 
5470d76bb7a0SLen Brown 	fd = open(path, O_RDONLY);
5471d76bb7a0SLen Brown 	if (fd < 0) {
5472d76bb7a0SLen Brown 		warn("fopen %s\n", path);
5473d76bb7a0SLen Brown 		return;
5474d76bb7a0SLen Brown 	}
5475d76bb7a0SLen Brown 
5476d76bb7a0SLen Brown 	retval = read(fd, (void *)&value, sizeof(int));
5477d76bb7a0SLen Brown 	if (retval != sizeof(int)) {
5478d76bb7a0SLen Brown 		warn("read %s\n", path);
5479d76bb7a0SLen Brown 		close(fd);
5480d76bb7a0SLen Brown 		return;
5481d76bb7a0SLen Brown 	}
54821b439f01SLen Brown 	fprintf(outf, "/dev/cpu_dma_latency: %d usec (%s)\n", value, value == 2000000000 ? "default" : "constrained");
5483d76bb7a0SLen Brown 
5484d76bb7a0SLen Brown 	close(fd);
5485d76bb7a0SLen Brown }
5486d76bb7a0SLen Brown 
54872af4f9b8SLen Brown /*
54882db0e5ebSJiang Jian  * Linux-perf manages the HW instructions-retired counter
54892af4f9b8SLen Brown  * by enabling when requested, and hiding rollover
54902af4f9b8SLen Brown  */
54912af4f9b8SLen Brown void linux_perf_init(void)
54922af4f9b8SLen Brown {
54932af4f9b8SLen Brown 	if (!BIC_IS_ENABLED(BIC_IPC))
54942af4f9b8SLen Brown 		return;
54952af4f9b8SLen Brown 
54962af4f9b8SLen Brown 	if (access("/proc/sys/kernel/perf_event_paranoid", F_OK))
54972af4f9b8SLen Brown 		return;
54982af4f9b8SLen Brown 
54992af4f9b8SLen Brown 	fd_instr_count_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
55002af4f9b8SLen Brown 	if (fd_instr_count_percpu == NULL)
55012af4f9b8SLen Brown 		err(-1, "calloc fd_instr_count_percpu");
55022af4f9b8SLen Brown 
55032af4f9b8SLen Brown 	BIC_PRESENT(BIC_IPC);
55042af4f9b8SLen Brown }
55052af4f9b8SLen Brown 
5506fcd17211SLen Brown void process_cpuid()
5507103a8feaSLen Brown {
550834041551SLen Brown 	unsigned int eax, ebx, ecx, edx;
550934041551SLen Brown 	unsigned int fms, family, model, stepping, ecx_flags, edx_flags;
5510b3a34e93SLen Brown 	unsigned int has_turbo;
5511ed0757b8SLen Brown 	unsigned long long ucode_patch = 0;
5512103a8feaSLen Brown 
5513103a8feaSLen Brown 	eax = ebx = ecx = edx = 0;
5514103a8feaSLen Brown 
55155aea2f7fSLen Brown 	__cpuid(0, max_level, ebx, ecx, edx);
5516103a8feaSLen Brown 
551734041551SLen Brown 	if (ebx == 0x756e6547 && ecx == 0x6c65746e && edx == 0x49656e69)
5518103a8feaSLen Brown 		genuine_intel = 1;
551934041551SLen Brown 	else if (ebx == 0x68747541 && ecx == 0x444d4163 && edx == 0x69746e65)
552034041551SLen Brown 		authentic_amd = 1;
5521c1c10cc7SPu Wen 	else if (ebx == 0x6f677948 && ecx == 0x656e6975 && edx == 0x6e65476e)
5522c1c10cc7SPu Wen 		hygon_genuine = 1;
5523103a8feaSLen Brown 
552496e47158SLen Brown 	if (!quiet)
5525ed0757b8SLen Brown 		fprintf(outf, "CPUID(0): %.4s%.4s%.4s 0x%x CPUID levels\n",
5526ed0757b8SLen Brown 			(char *)&ebx, (char *)&edx, (char *)&ecx, max_level);
5527103a8feaSLen Brown 
55285aea2f7fSLen Brown 	__cpuid(1, fms, ebx, ecx, edx);
5529103a8feaSLen Brown 	family = (fms >> 8) & 0xf;
5530103a8feaSLen Brown 	model = (fms >> 4) & 0xf;
5531103a8feaSLen Brown 	stepping = fms & 0xf;
55325aa3d1a2SCalvin Walton 	if (family == 0xf)
55335aa3d1a2SCalvin Walton 		family += (fms >> 20) & 0xff;
55345aa3d1a2SCalvin Walton 	if (family >= 6)
5535103a8feaSLen Brown 		model += ((fms >> 16) & 0xf) << 4;
553634041551SLen Brown 	ecx_flags = ecx;
553734041551SLen Brown 	edx_flags = edx;
5538103a8feaSLen Brown 
5539ed0757b8SLen Brown 	if (get_msr(sched_getcpu(), MSR_IA32_UCODE_REV, &ucode_patch))
5540ed0757b8SLen Brown 		warnx("get_msr(UCODE)\n");
5541ed0757b8SLen Brown 
5542103a8feaSLen Brown 	/*
5543103a8feaSLen Brown 	 * check max extended function levels of CPUID.
5544103a8feaSLen Brown 	 * This is needed to check for invariant TSC.
5545103a8feaSLen Brown 	 * This check is valid for both Intel and AMD.
5546103a8feaSLen Brown 	 */
5547103a8feaSLen Brown 	ebx = ecx = edx = 0;
55485aea2f7fSLen Brown 	__cpuid(0x80000000, max_extended_level, ebx, ecx, edx);
5549103a8feaSLen Brown 
555034041551SLen Brown 	if (!quiet) {
5551ed0757b8SLen Brown 		fprintf(outf, "CPUID(1): family:model:stepping 0x%x:%x:%x (%d:%d:%d) microcode 0x%x\n",
55521b439f01SLen Brown 			family, model, stepping, family, model, stepping,
55531b439f01SLen Brown 			(unsigned int)((ucode_patch >> 32) & 0xFFFFFFFF));
5554ed0757b8SLen Brown 		fprintf(outf, "CPUID(0x80000000): max_extended_levels: 0x%x\n", max_extended_level);
555534041551SLen Brown 		fprintf(outf, "CPUID(1): %s %s %s %s %s %s %s %s %s %s\n",
555634041551SLen Brown 			ecx_flags & (1 << 0) ? "SSE3" : "-",
555734041551SLen Brown 			ecx_flags & (1 << 3) ? "MONITOR" : "-",
555834041551SLen Brown 			ecx_flags & (1 << 6) ? "SMX" : "-",
555934041551SLen Brown 			ecx_flags & (1 << 7) ? "EIST" : "-",
556034041551SLen Brown 			ecx_flags & (1 << 8) ? "TM2" : "-",
556134041551SLen Brown 			edx_flags & (1 << 4) ? "TSC" : "-",
556234041551SLen Brown 			edx_flags & (1 << 5) ? "MSR" : "-",
556334041551SLen Brown 			edx_flags & (1 << 22) ? "ACPI-TM" : "-",
55641b439f01SLen Brown 			edx_flags & (1 << 28) ? "HT" : "-", edx_flags & (1 << 29) ? "TM" : "-");
556534041551SLen Brown 	}
556625368d7cSChen Yu 	if (genuine_intel) {
5567e9d3092fSZhang Rui 		model_orig = model;
5568f5a4c76aSLen Brown 		model = intel_model_duplicates(model);
556925368d7cSChen Yu 	}
557034041551SLen Brown 
557134041551SLen Brown 	if (!(edx_flags & (1 << 5)))
557234041551SLen Brown 		errx(1, "CPUID: no MSR");
557334041551SLen Brown 
557461a87ba7SLen Brown 	if (max_extended_level >= 0x80000007) {
5575103a8feaSLen Brown 
5576103a8feaSLen Brown 		/*
5577103a8feaSLen Brown 		 * Non-Stop TSC is advertised by CPUID.EAX=0x80000007: EDX.bit8
5578103a8feaSLen Brown 		 * this check is valid for both Intel and AMD
5579103a8feaSLen Brown 		 */
55805aea2f7fSLen Brown 		__cpuid(0x80000007, eax, ebx, ecx, edx);
55818209e054SThomas Renninger 		has_invariant_tsc = edx & (1 << 8);
5582d7899447SLen Brown 	}
5583103a8feaSLen Brown 
5584103a8feaSLen Brown 	/*
5585103a8feaSLen Brown 	 * APERF/MPERF is advertised by CPUID.EAX=0x6: ECX.bit0
5586103a8feaSLen Brown 	 * this check is valid for both Intel and AMD
5587103a8feaSLen Brown 	 */
5588103a8feaSLen Brown 
55895aea2f7fSLen Brown 	__cpuid(0x6, eax, ebx, ecx, edx);
55908209e054SThomas Renninger 	has_aperf = ecx & (1 << 0);
5591812db3f7SLen Brown 	if (has_aperf) {
5592812db3f7SLen Brown 		BIC_PRESENT(BIC_Avg_MHz);
5593812db3f7SLen Brown 		BIC_PRESENT(BIC_Busy);
5594812db3f7SLen Brown 		BIC_PRESENT(BIC_Bzy_MHz);
5595812db3f7SLen Brown 	}
5596889facbeSLen Brown 	do_dts = eax & (1 << 0);
5597812db3f7SLen Brown 	if (do_dts)
5598812db3f7SLen Brown 		BIC_PRESENT(BIC_CoreTmp);
5599b3a34e93SLen Brown 	has_turbo = eax & (1 << 1);
5600889facbeSLen Brown 	do_ptm = eax & (1 << 6);
5601812db3f7SLen Brown 	if (do_ptm)
5602812db3f7SLen Brown 		BIC_PRESENT(BIC_PkgTmp);
56037f5c258eSLen Brown 	has_hwp = eax & (1 << 7);
56047f5c258eSLen Brown 	has_hwp_notify = eax & (1 << 8);
56057f5c258eSLen Brown 	has_hwp_activity_window = eax & (1 << 9);
56067f5c258eSLen Brown 	has_hwp_epp = eax & (1 << 10);
56077f5c258eSLen Brown 	has_hwp_pkg = eax & (1 << 11);
5608889facbeSLen Brown 	has_epb = ecx & (1 << 3);
5609889facbeSLen Brown 
561096e47158SLen Brown 	if (!quiet)
5611b3a34e93SLen Brown 		fprintf(outf, "CPUID(6): %sAPERF, %sTURBO, %sDTS, %sPTM, %sHWP, "
56127f5c258eSLen Brown 			"%sHWPnotify, %sHWPwindow, %sHWPepp, %sHWPpkg, %sEPB\n",
56137f5c258eSLen Brown 			has_aperf ? "" : "No-",
5614b3a34e93SLen Brown 			has_turbo ? "" : "No-",
56157f5c258eSLen Brown 			do_dts ? "" : "No-",
56167f5c258eSLen Brown 			do_ptm ? "" : "No-",
56177f5c258eSLen Brown 			has_hwp ? "" : "No-",
56187f5c258eSLen Brown 			has_hwp_notify ? "" : "No-",
56197f5c258eSLen Brown 			has_hwp_activity_window ? "" : "No-",
56201b439f01SLen Brown 			has_hwp_epp ? "" : "No-", has_hwp_pkg ? "" : "No-", has_epb ? "" : "No-");
5621103a8feaSLen Brown 
562296e47158SLen Brown 	if (!quiet)
562369807a63SLen Brown 		decode_misc_enable_msr();
562469807a63SLen Brown 
562596e47158SLen Brown 	if (max_level >= 0x7 && !quiet) {
5626aa8d8cc7SLen Brown 		int has_sgx;
5627aa8d8cc7SLen Brown 
5628aa8d8cc7SLen Brown 		ecx = 0;
5629aa8d8cc7SLen Brown 
5630aa8d8cc7SLen Brown 		__cpuid_count(0x7, 0, eax, ebx, ecx, edx);
5631aa8d8cc7SLen Brown 
5632aa8d8cc7SLen Brown 		has_sgx = ebx & (1 << 2);
5633774627c5SLen Brown 
5634774627c5SLen Brown 		is_hybrid = edx & (1 << 15);
5635774627c5SLen Brown 
5636774627c5SLen Brown 		fprintf(outf, "CPUID(7): %sSGX %sHybrid\n", has_sgx ? "" : "No-", is_hybrid ? "" : "No-");
5637aa8d8cc7SLen Brown 
5638aa8d8cc7SLen Brown 		if (has_sgx)
5639aa8d8cc7SLen Brown 			decode_feature_control_msr();
5640aa8d8cc7SLen Brown 	}
5641aa8d8cc7SLen Brown 
564261a87ba7SLen Brown 	if (max_level >= 0x15) {
56438a5bdf41SLen Brown 		unsigned int eax_crystal;
56448a5bdf41SLen Brown 		unsigned int ebx_tsc;
56458a5bdf41SLen Brown 
56468a5bdf41SLen Brown 		/*
56478a5bdf41SLen Brown 		 * CPUID 15H TSC/Crystal ratio, possibly Crystal Hz
56488a5bdf41SLen Brown 		 */
56498a5bdf41SLen Brown 		eax_crystal = ebx_tsc = crystal_hz = edx = 0;
56505aea2f7fSLen Brown 		__cpuid(0x15, eax_crystal, ebx_tsc, crystal_hz, edx);
56518a5bdf41SLen Brown 
56528a5bdf41SLen Brown 		if (ebx_tsc != 0) {
56538a5bdf41SLen Brown 
565496e47158SLen Brown 			if (!quiet && (ebx != 0))
5655b7d8c148SLen Brown 				fprintf(outf, "CPUID(0x15): eax_crystal: %d ebx_tsc: %d ecx_crystal_hz: %d\n",
56568a5bdf41SLen Brown 					eax_crystal, ebx_tsc, crystal_hz);
56578a5bdf41SLen Brown 
56588a5bdf41SLen Brown 			if (crystal_hz == 0)
56598a5bdf41SLen Brown 				switch (model) {
5660af239c44SPeter Zijlstra 				case INTEL_FAM6_SKYLAKE_L:	/* SKL */
5661e8efbc80SLen Brown 					crystal_hz = 24000000;	/* 24.0 MHz */
5662e8efbc80SLen Brown 					break;
56635ebb34edSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_D:	/* DNV */
5664ec53e594SLen Brown 					crystal_hz = 25000000;	/* 25.0 MHz */
5665ec53e594SLen Brown 					break;
5666869ce69eSLen Brown 				case INTEL_FAM6_ATOM_GOLDMONT:	/* BXT */
5667f2c4db1bSPeter Zijlstra 				case INTEL_FAM6_ATOM_GOLDMONT_PLUS:
5668e8efbc80SLen Brown 					crystal_hz = 19200000;	/* 19.2 MHz */
56698a5bdf41SLen Brown 					break;
56708a5bdf41SLen Brown 				default:
56718a5bdf41SLen Brown 					crystal_hz = 0;
56728a5bdf41SLen Brown 				}
56738a5bdf41SLen Brown 
56748a5bdf41SLen Brown 			if (crystal_hz) {
56758a5bdf41SLen Brown 				tsc_hz = (unsigned long long)crystal_hz *ebx_tsc / eax_crystal;
567696e47158SLen Brown 				if (!quiet)
5677b7d8c148SLen Brown 					fprintf(outf, "TSC: %lld MHz (%d Hz * %d / %d / 1000000)\n",
56788a5bdf41SLen Brown 						tsc_hz / 1000000, crystal_hz, ebx_tsc, eax_crystal);
56798a5bdf41SLen Brown 			}
56808a5bdf41SLen Brown 		}
56818a5bdf41SLen Brown 	}
568261a87ba7SLen Brown 	if (max_level >= 0x16) {
568361a87ba7SLen Brown 		unsigned int base_mhz, max_mhz, bus_mhz, edx;
568461a87ba7SLen Brown 
568561a87ba7SLen Brown 		/*
568661a87ba7SLen Brown 		 * CPUID 16H Base MHz, Max MHz, Bus MHz
568761a87ba7SLen Brown 		 */
568861a87ba7SLen Brown 		base_mhz = max_mhz = bus_mhz = edx = 0;
568961a87ba7SLen Brown 
56905aea2f7fSLen Brown 		__cpuid(0x16, base_mhz, max_mhz, bus_mhz, edx);
569196e47158SLen Brown 		if (!quiet)
5692b7d8c148SLen Brown 			fprintf(outf, "CPUID(0x16): base_mhz: %d max_mhz: %d bus_mhz: %d\n",
569361a87ba7SLen Brown 				base_mhz, max_mhz, bus_mhz);
569461a87ba7SLen Brown 	}
56958a5bdf41SLen Brown 
5696b2b34dfeSHubert Chrzaniuk 	if (has_aperf)
5697b2b34dfeSHubert Chrzaniuk 		aperf_mperf_multiplier = get_aperf_mperf_multiplier(family, model);
5698b2b34dfeSHubert Chrzaniuk 
5699812db3f7SLen Brown 	BIC_PRESENT(BIC_IRQ);
5700812db3f7SLen Brown 	BIC_PRESENT(BIC_TSC_MHz);
5701812db3f7SLen Brown 
5702812db3f7SLen Brown 	if (probe_nhm_msrs(family, model)) {
5703812db3f7SLen Brown 		do_nhm_platform_info = 1;
5704812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
5705812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c3);
5706812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c6);
5707812db3f7SLen Brown 		BIC_PRESENT(BIC_SMI);
5708812db3f7SLen Brown 	}
5709d7899447SLen Brown 	do_snb_cstates = has_snb_msrs(family, model);
5710812db3f7SLen Brown 
5711812db3f7SLen Brown 	if (do_snb_cstates)
5712812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU_c7);
5713812db3f7SLen Brown 
57145a63426eSLen Brown 	do_irtl_snb = has_snb_msrs(family, model);
57150f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__2))
57160f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc2);
57170f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__3)
57180f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc3);
57190f47c08dSLen Brown 	if (pkg_cstate_limit >= PCL__6)
57200f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
57210f47c08dSLen Brown 	if (do_snb_cstates && (pkg_cstate_limit >= PCL__7))
57220f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc7);
57230539ba11SLen Brown 	if (has_slv_msrs(family, model)) {
57240f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc2);
57250f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
57260f47c08dSLen Brown 		BIC_PRESENT(BIC_Pkgpc6);
57270f47c08dSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
57280539ba11SLen Brown 		BIC_PRESENT(BIC_Mod_c6);
57290539ba11SLen Brown 		use_c1_residency_msr = 1;
57300539ba11SLen Brown 	}
573120de0dabSAntti Laakso 	if (is_jvl(family, model)) {
573220de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_CPU_c3);
573320de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_CPU_c7);
573420de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc2);
573520de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc3);
573620de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc6);
573720de0dabSAntti Laakso 		BIC_NOT_PRESENT(BIC_Pkgpc7);
573820de0dabSAntti Laakso 	}
57397170a374SLen Brown 	if (is_dnv(family, model)) {
57407170a374SLen Brown 		BIC_PRESENT(BIC_CPU_c1);
57417170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
57427170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
57437170a374SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
57447170a374SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
57457170a374SLen Brown 		use_c1_residency_msr = 1;
57467170a374SLen Brown 	}
5747684e40e9SArtem Bityutskiy 	if (is_skx(family, model) || is_icx(family, model) || is_spr(family, model)) {
574834c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
574934c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc3);
575034c76197SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
575134c76197SLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
575234c76197SLen Brown 	}
5753ade0ebacSLen Brown 	if (is_bdx(family, model)) {
5754ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c7);
5755ade0ebacSLen Brown 		BIC_NOT_PRESENT(BIC_Pkgpc7);
5756ade0ebacSLen Brown 	}
5757570992fcSLen Brown 	if (has_c8910_msrs(family, model)) {
5758c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL__8)
57590f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc8);
5760c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL__9)
57610f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc9);
5762c315a09bSLen Brown 		if (pkg_cstate_limit >= PCL_10)
57630f47c08dSLen Brown 			BIC_PRESENT(BIC_Pkgpc10);
57640f47c08dSLen Brown 	}
5765570992fcSLen Brown 	do_irtl_hsw = has_c8910_msrs(family, model);
5766a99d8730SLen Brown 	if (has_skl_msrs(family, model)) {
5767a99d8730SLen Brown 		BIC_PRESENT(BIC_Totl_c0);
5768a99d8730SLen Brown 		BIC_PRESENT(BIC_Any_c0);
5769a99d8730SLen Brown 		BIC_PRESENT(BIC_GFX_c0);
5770a99d8730SLen Brown 		BIC_PRESENT(BIC_CPUGFX);
5771a99d8730SLen Brown 	}
5772144b44b1SLen Brown 	do_slm_cstates = is_slm(family, model);
5773fb5d4327SDasaratharaman Chandramouli 	do_knl_cstates = is_knl(family, model);
5774103a8feaSLen Brown 
57751b439f01SLen Brown 	if (do_slm_cstates || do_knl_cstates || is_cnl(family, model) || is_ehl(family, model))
5776562855eeSLen Brown 		BIC_NOT_PRESENT(BIC_CPU_c3);
5777103a8feaSLen Brown 
577896e47158SLen Brown 	if (!quiet)
5779f0057310SLen Brown 		decode_misc_pwr_mgmt_msr();
5780f0057310SLen Brown 
578196e47158SLen Brown 	if (!quiet && has_slv_msrs(family, model))
578271616c8eSLen Brown 		decode_c6_demotion_policy_msr();
578371616c8eSLen Brown 
5784889facbeSLen Brown 	rapl_probe(family, model);
57853a9a941dSLen Brown 	perf_limit_reasons_probe(family, model);
5786ac980e13SArtem Bityutskiy 	automatic_cstate_conversion_probe(family, model);
5787889facbeSLen Brown 
57880b9a0b9bSZhang Rui 	check_tcc_offset(model_orig);
57890b9a0b9bSZhang Rui 
579096e47158SLen Brown 	if (!quiet)
57911b69317dSColin Ian King 		dump_cstate_pstate_config_info(family, model);
5792a5c6d65dSLen Brown 	intel_uncore_frequency_probe();
5793fcd17211SLen Brown 
579441618e63SLen Brown 	if (!quiet)
5795d76bb7a0SLen Brown 		print_dev_latency();
5796d76bb7a0SLen Brown 	if (!quiet)
579741618e63SLen Brown 		dump_sysfs_cstate_config();
57987293fccdSLen Brown 	if (!quiet)
57997293fccdSLen Brown 		dump_sysfs_pstate_config();
580041618e63SLen Brown 
58018c69da29SChen Yu 	if (has_skl_msrs(family, model) || is_ehl(family, model))
5802a2b7b749SLen Brown 		calculate_tsc_tweak();
5803a2b7b749SLen Brown 
5804812db3f7SLen Brown 	if (!access("/sys/class/drm/card0/power/rc6_residency_ms", R_OK))
5805812db3f7SLen Brown 		BIC_PRESENT(BIC_GFX_rc6);
5806fdf676e5SLen Brown 
5807812db3f7SLen Brown 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_cur_freq_mhz", R_OK))
5808812db3f7SLen Brown 		BIC_PRESENT(BIC_GFXMHz);
580927d47356SLen Brown 
5810b4b91569SRafael Antognolli 	if (!access("/sys/class/graphics/fb0/device/drm/card0/gt_act_freq_mhz", R_OK))
5811b4b91569SRafael Antognolli 		BIC_PRESENT(BIC_GFXACTMHz);
5812b4b91569SRafael Antognolli 
5813be0e54c4SLen Brown 	if (!access("/sys/devices/system/cpu/cpuidle/low_power_idle_cpu_residency_us", R_OK))
5814be0e54c4SLen Brown 		BIC_PRESENT(BIC_CPU_LPI);
5815be0e54c4SLen Brown 	else
5816be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_CPU_LPI);
5817be0e54c4SLen Brown 
5818eae97e05SChen Yu 	if (!access("/sys/devices/system/cpu/cpu0/thermal_throttle/core_throttle_count", R_OK))
5819eae97e05SChen Yu 		BIC_PRESENT(BIC_CORE_THROT_CNT);
5820eae97e05SChen Yu 	else
5821eae97e05SChen Yu 		BIC_NOT_PRESENT(BIC_CORE_THROT_CNT);
5822eae97e05SChen Yu 
58231f81c5efSLen Brown 	if (!access(sys_lpi_file_sysfs, R_OK)) {
58241f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_sysfs;
5825be0e54c4SLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
58261f81c5efSLen Brown 	} else if (!access(sys_lpi_file_debugfs, R_OK)) {
58271f81c5efSLen Brown 		sys_lpi_file = sys_lpi_file_debugfs;
58281f81c5efSLen Brown 		BIC_PRESENT(BIC_SYS_LPI);
58291f81c5efSLen Brown 	} else {
58301f81c5efSLen Brown 		sys_lpi_file_sysfs = NULL;
5831be0e54c4SLen Brown 		BIC_NOT_PRESENT(BIC_SYS_LPI);
58321f81c5efSLen Brown 	}
5833be0e54c4SLen Brown 
583496e47158SLen Brown 	if (!quiet)
583533148d67SLen Brown 		decode_misc_feature_control();
583633148d67SLen Brown 
5837889facbeSLen Brown 	return;
5838103a8feaSLen Brown }
5839103a8feaSLen Brown 
5840103a8feaSLen Brown /*
5841103a8feaSLen Brown  * in /dev/cpu/ return success for names that are numbers
5842103a8feaSLen Brown  * ie. filter out ".", "..", "microcode".
5843103a8feaSLen Brown  */
5844103a8feaSLen Brown int dir_filter(const struct dirent *dirp)
5845103a8feaSLen Brown {
5846103a8feaSLen Brown 	if (isdigit(dirp->d_name[0]))
5847103a8feaSLen Brown 		return 1;
5848103a8feaSLen Brown 	else
5849103a8feaSLen Brown 		return 0;
5850103a8feaSLen Brown }
5851103a8feaSLen Brown 
5852c98d5d94SLen Brown void topology_probe()
5853c98d5d94SLen Brown {
5854c98d5d94SLen Brown 	int i;
5855c98d5d94SLen Brown 	int max_core_id = 0;
5856c98d5d94SLen Brown 	int max_package_id = 0;
58576de68fe1SLen Brown 	int max_die_id = 0;
5858c98d5d94SLen Brown 	int max_siblings = 0;
5859c98d5d94SLen Brown 
5860c98d5d94SLen Brown 	/* Initialize num_cpus, max_cpu_num */
5861843c5791SPrarit Bhargava 	set_max_cpu_num();
5862c98d5d94SLen Brown 	topo.num_cpus = 0;
5863c98d5d94SLen Brown 	for_all_proc_cpus(count_cpus);
5864c98d5d94SLen Brown 	if (!summary_only && topo.num_cpus > 1)
5865812db3f7SLen Brown 		BIC_PRESENT(BIC_CPU);
5866c98d5d94SLen Brown 
5867d8af6f5fSLen Brown 	if (debug > 1)
5868b7d8c148SLen Brown 		fprintf(outf, "num_cpus %d max_cpu_num %d\n", topo.num_cpus, topo.max_cpu_num);
5869c98d5d94SLen Brown 
5870c98d5d94SLen Brown 	cpus = calloc(1, (topo.max_cpu_num + 1) * sizeof(struct cpu_topology));
5871b2c95d90SJosh Triplett 	if (cpus == NULL)
5872b2c95d90SJosh Triplett 		err(1, "calloc cpus");
5873c98d5d94SLen Brown 
5874c98d5d94SLen Brown 	/*
5875c98d5d94SLen Brown 	 * Allocate and initialize cpu_present_set
5876c98d5d94SLen Brown 	 */
5877c98d5d94SLen Brown 	cpu_present_set = CPU_ALLOC((topo.max_cpu_num + 1));
5878b2c95d90SJosh Triplett 	if (cpu_present_set == NULL)
5879b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5880c98d5d94SLen Brown 	cpu_present_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5881c98d5d94SLen Brown 	CPU_ZERO_S(cpu_present_setsize, cpu_present_set);
5882c98d5d94SLen Brown 	for_all_proc_cpus(mark_cpu_present);
5883c98d5d94SLen Brown 
5884c98d5d94SLen Brown 	/*
58851ef7d21aSLen Brown 	 * Validate that all cpus in cpu_subset are also in cpu_present_set
58861ef7d21aSLen Brown 	 */
58871ef7d21aSLen Brown 	for (i = 0; i < CPU_SUBSET_MAXCPUS; ++i) {
58881ef7d21aSLen Brown 		if (CPU_ISSET_S(i, cpu_subset_size, cpu_subset))
58891ef7d21aSLen Brown 			if (!CPU_ISSET_S(i, cpu_present_setsize, cpu_present_set))
58901ef7d21aSLen Brown 				err(1, "cpu%d not present", i);
58911ef7d21aSLen Brown 	}
58921ef7d21aSLen Brown 
58931ef7d21aSLen Brown 	/*
5894c98d5d94SLen Brown 	 * Allocate and initialize cpu_affinity_set
5895c98d5d94SLen Brown 	 */
5896c98d5d94SLen Brown 	cpu_affinity_set = CPU_ALLOC((topo.max_cpu_num + 1));
5897b2c95d90SJosh Triplett 	if (cpu_affinity_set == NULL)
5898b2c95d90SJosh Triplett 		err(3, "CPU_ALLOC");
5899c98d5d94SLen Brown 	cpu_affinity_setsize = CPU_ALLOC_SIZE((topo.max_cpu_num + 1));
5900c98d5d94SLen Brown 	CPU_ZERO_S(cpu_affinity_setsize, cpu_affinity_set);
5901c98d5d94SLen Brown 
59028cb48b32SPrarit Bhargava 	for_all_proc_cpus(init_thread_id);
5903c98d5d94SLen Brown 
5904c98d5d94SLen Brown 	/*
5905c98d5d94SLen Brown 	 * For online cpus
5906c98d5d94SLen Brown 	 * find max_core_id, max_package_id
5907c98d5d94SLen Brown 	 */
5908c98d5d94SLen Brown 	for (i = 0; i <= topo.max_cpu_num; ++i) {
5909c98d5d94SLen Brown 		int siblings;
5910c98d5d94SLen Brown 
5911c98d5d94SLen Brown 		if (cpu_is_not_present(i)) {
5912d8af6f5fSLen Brown 			if (debug > 1)
5913b7d8c148SLen Brown 				fprintf(outf, "cpu%d NOT PRESENT\n", i);
5914c98d5d94SLen Brown 			continue;
5915c98d5d94SLen Brown 		}
5916c98d5d94SLen Brown 
59170e2d8f05SLen Brown 		cpus[i].logical_cpu_id = i;
59180e2d8f05SLen Brown 
59190e2d8f05SLen Brown 		/* get package information */
5920c98d5d94SLen Brown 		cpus[i].physical_package_id = get_physical_package_id(i);
5921c98d5d94SLen Brown 		if (cpus[i].physical_package_id > max_package_id)
5922c98d5d94SLen Brown 			max_package_id = cpus[i].physical_package_id;
5923c98d5d94SLen Brown 
59246de68fe1SLen Brown 		/* get die information */
59256de68fe1SLen Brown 		cpus[i].die_id = get_die_id(i);
59266de68fe1SLen Brown 		if (cpus[i].die_id > max_die_id)
59276de68fe1SLen Brown 			max_die_id = cpus[i].die_id;
59286de68fe1SLen Brown 
59290e2d8f05SLen Brown 		/* get numa node information */
5930ef605741SPrarit Bhargava 		cpus[i].physical_node_id = get_physical_node_id(&cpus[i]);
5931ef605741SPrarit Bhargava 		if (cpus[i].physical_node_id > topo.max_node_num)
5932ef605741SPrarit Bhargava 			topo.max_node_num = cpus[i].physical_node_id;
59330e2d8f05SLen Brown 
59340e2d8f05SLen Brown 		/* get core information */
59350e2d8f05SLen Brown 		cpus[i].physical_core_id = get_core_id(i);
59360e2d8f05SLen Brown 		if (cpus[i].physical_core_id > max_core_id)
59370e2d8f05SLen Brown 			max_core_id = cpus[i].physical_core_id;
59380e2d8f05SLen Brown 
59390e2d8f05SLen Brown 		/* get thread information */
59400e2d8f05SLen Brown 		siblings = get_thread_siblings(&cpus[i]);
5941c98d5d94SLen Brown 		if (siblings > max_siblings)
5942c98d5d94SLen Brown 			max_siblings = siblings;
59434f206a0fSArtem Bityutskiy 		if (cpus[i].thread_id == 0)
59448cb48b32SPrarit Bhargava 			topo.num_cores++;
5945c98d5d94SLen Brown 	}
5946ef605741SPrarit Bhargava 
594770a9c6e8SPrarit Bhargava 	topo.cores_per_node = max_core_id + 1;
5948d8af6f5fSLen Brown 	if (debug > 1)
59491b439f01SLen Brown 		fprintf(outf, "max_core_id %d, sizing for %d cores per package\n", max_core_id, topo.cores_per_node);
595070a9c6e8SPrarit Bhargava 	if (!summary_only && topo.cores_per_node > 1)
5951812db3f7SLen Brown 		BIC_PRESENT(BIC_Core);
5952c98d5d94SLen Brown 
59536de68fe1SLen Brown 	topo.num_die = max_die_id + 1;
59546de68fe1SLen Brown 	if (debug > 1)
59551b439f01SLen Brown 		fprintf(outf, "max_die_id %d, sizing for %d die\n", max_die_id, topo.num_die);
59566de68fe1SLen Brown 	if (!summary_only && topo.num_die > 1)
59576de68fe1SLen Brown 		BIC_PRESENT(BIC_Die);
59586de68fe1SLen Brown 
5959c98d5d94SLen Brown 	topo.num_packages = max_package_id + 1;
5960d8af6f5fSLen Brown 	if (debug > 1)
59611b439f01SLen Brown 		fprintf(outf, "max_package_id %d, sizing for %d packages\n", max_package_id, topo.num_packages);
59627da6e3e2SLen Brown 	if (!summary_only && topo.num_packages > 1)
5963812db3f7SLen Brown 		BIC_PRESENT(BIC_Package);
5964c98d5d94SLen Brown 
5965ef605741SPrarit Bhargava 	set_node_data();
5966ef605741SPrarit Bhargava 	if (debug > 1)
596770a9c6e8SPrarit Bhargava 		fprintf(outf, "nodes_per_pkg %d\n", topo.nodes_per_pkg);
596801235041SPrarit Bhargava 	if (!summary_only && topo.nodes_per_pkg > 1)
596901235041SPrarit Bhargava 		BIC_PRESENT(BIC_Node);
5970ef605741SPrarit Bhargava 
597170a9c6e8SPrarit Bhargava 	topo.threads_per_core = max_siblings;
5972d8af6f5fSLen Brown 	if (debug > 1)
5973b7d8c148SLen Brown 		fprintf(outf, "max_siblings %d\n", max_siblings);
59742ffbb224SPrarit Bhargava 
59752ffbb224SPrarit Bhargava 	if (debug < 1)
59762ffbb224SPrarit Bhargava 		return;
59772ffbb224SPrarit Bhargava 
59782ffbb224SPrarit Bhargava 	for (i = 0; i <= topo.max_cpu_num; ++i) {
59790ec712e3SLen Brown 		if (cpu_is_not_present(i))
59800ec712e3SLen Brown 			continue;
59812ffbb224SPrarit Bhargava 		fprintf(outf,
59826de68fe1SLen Brown 			"cpu %d pkg %d die %d node %d lnode %d core %d thread %d\n",
59836de68fe1SLen Brown 			i, cpus[i].physical_package_id, cpus[i].die_id,
59841b439f01SLen Brown 			cpus[i].physical_node_id, cpus[i].logical_node_id, cpus[i].physical_core_id, cpus[i].thread_id);
59852ffbb224SPrarit Bhargava 	}
59862ffbb224SPrarit Bhargava 
5987c98d5d94SLen Brown }
5988c98d5d94SLen Brown 
59891b439f01SLen Brown void allocate_counters(struct thread_data **t, struct core_data **c, struct pkg_data **p)
5990c98d5d94SLen Brown {
5991c98d5d94SLen Brown 	int i;
59921b439f01SLen Brown 	int num_cores = topo.cores_per_node * topo.nodes_per_pkg * topo.num_packages;
599340f5cfe7SPrarit Bhargava 	int num_threads = topo.threads_per_core * num_cores;
5994c98d5d94SLen Brown 
599540f5cfe7SPrarit Bhargava 	*t = calloc(num_threads, sizeof(struct thread_data));
5996c98d5d94SLen Brown 	if (*t == NULL)
5997c98d5d94SLen Brown 		goto error;
5998c98d5d94SLen Brown 
599940f5cfe7SPrarit Bhargava 	for (i = 0; i < num_threads; i++)
6000c98d5d94SLen Brown 		(*t)[i].cpu_id = -1;
6001c98d5d94SLen Brown 
600240f5cfe7SPrarit Bhargava 	*c = calloc(num_cores, sizeof(struct core_data));
6003c98d5d94SLen Brown 	if (*c == NULL)
6004c98d5d94SLen Brown 		goto error;
6005c98d5d94SLen Brown 
600640f5cfe7SPrarit Bhargava 	for (i = 0; i < num_cores; i++)
6007c98d5d94SLen Brown 		(*c)[i].core_id = -1;
6008c98d5d94SLen Brown 
6009678a3bd1SLen Brown 	*p = calloc(topo.num_packages, sizeof(struct pkg_data));
6010c98d5d94SLen Brown 	if (*p == NULL)
6011c98d5d94SLen Brown 		goto error;
6012c98d5d94SLen Brown 
6013c98d5d94SLen Brown 	for (i = 0; i < topo.num_packages; i++)
6014c98d5d94SLen Brown 		(*p)[i].package_id = i;
6015c98d5d94SLen Brown 
6016c98d5d94SLen Brown 	return;
6017c98d5d94SLen Brown error:
6018b2c95d90SJosh Triplett 	err(1, "calloc counters");
6019c98d5d94SLen Brown }
60201b439f01SLen Brown 
6021c98d5d94SLen Brown /*
6022c98d5d94SLen Brown  * init_counter()
6023c98d5d94SLen Brown  *
6024c98d5d94SLen Brown  * set FIRST_THREAD_IN_CORE and FIRST_CORE_IN_PACKAGE
6025c98d5d94SLen Brown  */
60261b439f01SLen Brown void init_counter(struct thread_data *thread_base, struct core_data *core_base, struct pkg_data *pkg_base, int cpu_id)
6027c98d5d94SLen Brown {
60288cb48b32SPrarit Bhargava 	int pkg_id = cpus[cpu_id].physical_package_id;
602940f5cfe7SPrarit Bhargava 	int node_id = cpus[cpu_id].logical_node_id;
60308cb48b32SPrarit Bhargava 	int core_id = cpus[cpu_id].physical_core_id;
60318cb48b32SPrarit Bhargava 	int thread_id = cpus[cpu_id].thread_id;
6032c98d5d94SLen Brown 	struct thread_data *t;
6033c98d5d94SLen Brown 	struct core_data *c;
6034c98d5d94SLen Brown 	struct pkg_data *p;
6035c98d5d94SLen Brown 
603642dd4520SNathan Ciobanu 	/* Workaround for systems where physical_node_id==-1
603742dd4520SNathan Ciobanu 	 * and logical_node_id==(-1 - topo.num_cpus)
603842dd4520SNathan Ciobanu 	 */
603942dd4520SNathan Ciobanu 	if (node_id < 0)
604042dd4520SNathan Ciobanu 		node_id = 0;
604142dd4520SNathan Ciobanu 
604240f5cfe7SPrarit Bhargava 	t = GET_THREAD(thread_base, thread_id, core_id, node_id, pkg_id);
604340f5cfe7SPrarit Bhargava 	c = GET_CORE(core_base, core_id, node_id, pkg_id);
60448cb48b32SPrarit Bhargava 	p = GET_PKG(pkg_base, pkg_id);
6045c98d5d94SLen Brown 
6046c98d5d94SLen Brown 	t->cpu_id = cpu_id;
60478cb48b32SPrarit Bhargava 	if (thread_id == 0) {
6048c98d5d94SLen Brown 		t->flags |= CPU_IS_FIRST_THREAD_IN_CORE;
6049c98d5d94SLen Brown 		if (cpu_is_first_core_in_package(cpu_id))
6050c98d5d94SLen Brown 			t->flags |= CPU_IS_FIRST_CORE_IN_PACKAGE;
6051c98d5d94SLen Brown 	}
6052c98d5d94SLen Brown 
60538cb48b32SPrarit Bhargava 	c->core_id = core_id;
60548cb48b32SPrarit Bhargava 	p->package_id = pkg_id;
6055c98d5d94SLen Brown }
6056c98d5d94SLen Brown 
6057c98d5d94SLen Brown int initialize_counters(int cpu_id)
6058c98d5d94SLen Brown {
60598cb48b32SPrarit Bhargava 	init_counter(EVEN_COUNTERS, cpu_id);
60608cb48b32SPrarit Bhargava 	init_counter(ODD_COUNTERS, cpu_id);
6061c98d5d94SLen Brown 	return 0;
6062c98d5d94SLen Brown }
6063c98d5d94SLen Brown 
6064c98d5d94SLen Brown void allocate_output_buffer()
6065c98d5d94SLen Brown {
6066eeb71c95SNaoya Horiguchi 	output_buffer = calloc(1, (1 + topo.num_cpus) * 2048);
6067c98d5d94SLen Brown 	outp = output_buffer;
6068b2c95d90SJosh Triplett 	if (outp == NULL)
6069b2c95d90SJosh Triplett 		err(-1, "calloc output buffer");
6070c98d5d94SLen Brown }
60711b439f01SLen Brown 
607236229897SLen Brown void allocate_fd_percpu(void)
607336229897SLen Brown {
607401a67adfSMika Westerberg 	fd_percpu = calloc(topo.max_cpu_num + 1, sizeof(int));
607536229897SLen Brown 	if (fd_percpu == NULL)
607636229897SLen Brown 		err(-1, "calloc fd_percpu");
607736229897SLen Brown }
60781b439f01SLen Brown 
6079562a2d37SLen Brown void allocate_irq_buffers(void)
6080562a2d37SLen Brown {
6081562a2d37SLen Brown 	irq_column_2_cpu = calloc(topo.num_cpus, sizeof(int));
6082562a2d37SLen Brown 	if (irq_column_2_cpu == NULL)
6083562a2d37SLen Brown 		err(-1, "calloc %d", topo.num_cpus);
6084c98d5d94SLen Brown 
608501a67adfSMika Westerberg 	irqs_per_cpu = calloc(topo.max_cpu_num + 1, sizeof(int));
6086562a2d37SLen Brown 	if (irqs_per_cpu == NULL)
608701a67adfSMika Westerberg 		err(-1, "calloc %d", topo.max_cpu_num + 1);
6088562a2d37SLen Brown }
60891b439f01SLen Brown 
6090c98d5d94SLen Brown void setup_all_buffers(void)
6091c98d5d94SLen Brown {
6092c98d5d94SLen Brown 	topology_probe();
6093562a2d37SLen Brown 	allocate_irq_buffers();
609436229897SLen Brown 	allocate_fd_percpu();
6095c98d5d94SLen Brown 	allocate_counters(&thread_even, &core_even, &package_even);
6096c98d5d94SLen Brown 	allocate_counters(&thread_odd, &core_odd, &package_odd);
6097c98d5d94SLen Brown 	allocate_output_buffer();
6098c98d5d94SLen Brown 	for_all_proc_cpus(initialize_counters);
6099c98d5d94SLen Brown }
61003b4d5c7fSAndy Shevchenko 
61017ce7d5deSPrarit Bhargava void set_base_cpu(void)
61027ce7d5deSPrarit Bhargava {
61037ce7d5deSPrarit Bhargava 	base_cpu = sched_getcpu();
61047ce7d5deSPrarit Bhargava 	if (base_cpu < 0)
61057ce7d5deSPrarit Bhargava 		err(-ENODEV, "No valid cpus found");
61067ce7d5deSPrarit Bhargava 
61077ce7d5deSPrarit Bhargava 	if (debug > 1)
6108b7d8c148SLen Brown 		fprintf(outf, "base_cpu = %d\n", base_cpu);
61097ce7d5deSPrarit Bhargava }
61107ce7d5deSPrarit Bhargava 
6111103a8feaSLen Brown void turbostat_init()
6112103a8feaSLen Brown {
61137ce7d5deSPrarit Bhargava 	setup_all_buffers();
61147ce7d5deSPrarit Bhargava 	set_base_cpu();
6115103a8feaSLen Brown 	check_dev_msr();
611698481e79SLen Brown 	check_permissions();
6117fcd17211SLen Brown 	process_cpuid();
61182af4f9b8SLen Brown 	linux_perf_init();
6119103a8feaSLen Brown 
612096e47158SLen Brown 	if (!quiet)
61217f5c258eSLen Brown 		for_all_cpus(print_hwp, ODD_COUNTERS);
61227f5c258eSLen Brown 
612396e47158SLen Brown 	if (!quiet)
6124889facbeSLen Brown 		for_all_cpus(print_epb, ODD_COUNTERS);
6125889facbeSLen Brown 
612696e47158SLen Brown 	if (!quiet)
61273a9a941dSLen Brown 		for_all_cpus(print_perf_limit, ODD_COUNTERS);
61283a9a941dSLen Brown 
612996e47158SLen Brown 	if (!quiet)
6130889facbeSLen Brown 		for_all_cpus(print_rapl, ODD_COUNTERS);
6131889facbeSLen Brown 
6132889facbeSLen Brown 	for_all_cpus(set_temperature_target, ODD_COUNTERS);
6133889facbeSLen Brown 
61347ab5ff49SZhang Rui 	for_all_cpus(get_cpu_type, ODD_COUNTERS);
61357ab5ff49SZhang Rui 	for_all_cpus(get_cpu_type, EVEN_COUNTERS);
61367ab5ff49SZhang Rui 
613796e47158SLen Brown 	if (!quiet)
6138889facbeSLen Brown 		for_all_cpus(print_thermal, ODD_COUNTERS);
61395a63426eSLen Brown 
614096e47158SLen Brown 	if (!quiet && do_irtl_snb)
61415a63426eSLen Brown 		print_irtl();
6142164d7a96SLen Brown 
6143164d7a96SLen Brown 	if (DO_BIC(BIC_IPC))
6144164d7a96SLen Brown 		(void)get_instr_count_fd(base_cpu);
6145103a8feaSLen Brown }
6146103a8feaSLen Brown 
6147103a8feaSLen Brown int fork_it(char **argv)
6148103a8feaSLen Brown {
6149103a8feaSLen Brown 	pid_t child_pid;
6150d91bb17cSLen Brown 	int status;
6151d15cf7c1SLen Brown 
6152218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
6153d91bb17cSLen Brown 	status = for_all_cpus(get_counters, EVEN_COUNTERS);
61544c2122d4SLen Brown 	first_counter_read = 0;
6155d91bb17cSLen Brown 	if (status)
6156d91bb17cSLen Brown 		exit(status);
6157d15cf7c1SLen Brown 	/* clear affinity side-effect of get_counters() */
6158d15cf7c1SLen Brown 	sched_setaffinity(0, cpu_present_setsize, cpu_present_set);
6159103a8feaSLen Brown 	gettimeofday(&tv_even, (struct timezone *)NULL);
6160103a8feaSLen Brown 
6161103a8feaSLen Brown 	child_pid = fork();
6162103a8feaSLen Brown 	if (!child_pid) {
6163103a8feaSLen Brown 		/* child */
6164103a8feaSLen Brown 		execvp(argv[0], argv);
61650815a3d0SLen Brown 		err(errno, "exec %s", argv[0]);
6166103a8feaSLen Brown 	} else {
6167103a8feaSLen Brown 
6168103a8feaSLen Brown 		/* parent */
6169b2c95d90SJosh Triplett 		if (child_pid == -1)
6170b2c95d90SJosh Triplett 			err(1, "fork");
6171103a8feaSLen Brown 
6172103a8feaSLen Brown 		signal(SIGINT, SIG_IGN);
6173103a8feaSLen Brown 		signal(SIGQUIT, SIG_IGN);
6174b2c95d90SJosh Triplett 		if (waitpid(child_pid, &status, 0) == -1)
6175b2c95d90SJosh Triplett 			err(status, "waitpid");
61762a954966SDavid Arcari 
61772a954966SDavid Arcari 		if (WIFEXITED(status))
61782a954966SDavid Arcari 			status = WEXITSTATUS(status);
6179103a8feaSLen Brown 	}
6180c98d5d94SLen Brown 	/*
6181c98d5d94SLen Brown 	 * n.b. fork_it() does not check for errors from for_all_cpus()
6182c98d5d94SLen Brown 	 * because re-starting is problematic when forking
6183c98d5d94SLen Brown 	 */
6184218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
6185c98d5d94SLen Brown 	for_all_cpus(get_counters, ODD_COUNTERS);
6186103a8feaSLen Brown 	gettimeofday(&tv_odd, (struct timezone *)NULL);
6187103a8feaSLen Brown 	timersub(&tv_odd, &tv_even, &tv_delta);
6188ba3dec99SLen Brown 	if (for_all_cpus_2(delta_cpu, ODD_COUNTERS, EVEN_COUNTERS))
6189ba3dec99SLen Brown 		fprintf(outf, "%s: Counter reset detected\n", progname);
6190ba3dec99SLen Brown 	else {
6191c98d5d94SLen Brown 		compute_average(EVEN_COUNTERS);
6192c98d5d94SLen Brown 		format_all_counters(EVEN_COUNTERS);
6193ba3dec99SLen Brown 	}
6194103a8feaSLen Brown 
6195b7d8c148SLen Brown 	fprintf(outf, "%.6f sec\n", tv_delta.tv_sec + tv_delta.tv_usec / 1000000.0);
6196b7d8c148SLen Brown 
6197b7d8c148SLen Brown 	flush_output_stderr();
6198103a8feaSLen Brown 
6199d91bb17cSLen Brown 	return status;
6200103a8feaSLen Brown }
6201103a8feaSLen Brown 
62023b4d5c7fSAndy Shevchenko int get_and_dump_counters(void)
62033b4d5c7fSAndy Shevchenko {
62043b4d5c7fSAndy Shevchenko 	int status;
62053b4d5c7fSAndy Shevchenko 
6206218f0e8dSLen Brown 	snapshot_proc_sysfs_files();
62073b4d5c7fSAndy Shevchenko 	status = for_all_cpus(get_counters, ODD_COUNTERS);
62083b4d5c7fSAndy Shevchenko 	if (status)
62093b4d5c7fSAndy Shevchenko 		return status;
62103b4d5c7fSAndy Shevchenko 
62113b4d5c7fSAndy Shevchenko 	status = for_all_cpus(dump_counters, ODD_COUNTERS);
62123b4d5c7fSAndy Shevchenko 	if (status)
62133b4d5c7fSAndy Shevchenko 		return status;
62143b4d5c7fSAndy Shevchenko 
6215b7d8c148SLen Brown 	flush_output_stdout();
62163b4d5c7fSAndy Shevchenko 
62173b4d5c7fSAndy Shevchenko 	return status;
62183b4d5c7fSAndy Shevchenko }
62193b4d5c7fSAndy Shevchenko 
62201b439f01SLen Brown void print_version()
62211b439f01SLen Brown {
622258990892SLen Brown 	fprintf(outf, "turbostat version 2022.04.16 - Len Brown <lenb@kernel.org>\n");
6223d8af6f5fSLen Brown }
6224d8af6f5fSLen Brown 
622503331233SChen Yu #define COMMAND_LINE_SIZE 2048
622603331233SChen Yu 
622703331233SChen Yu void print_bootcmd(void)
622803331233SChen Yu {
622903331233SChen Yu 	char bootcmd[COMMAND_LINE_SIZE];
623003331233SChen Yu 	FILE *fp;
623103331233SChen Yu 	int ret;
623203331233SChen Yu 
623303331233SChen Yu 	memset(bootcmd, 0, COMMAND_LINE_SIZE);
623403331233SChen Yu 	fp = fopen("/proc/cmdline", "r");
623503331233SChen Yu 	if (!fp)
623603331233SChen Yu 		return;
623703331233SChen Yu 
623803331233SChen Yu 	ret = fread(bootcmd, sizeof(char), COMMAND_LINE_SIZE - 1, fp);
623903331233SChen Yu 	if (ret) {
624003331233SChen Yu 		bootcmd[ret] = '\0';
624103331233SChen Yu 		/* the last character is already '\n' */
624203331233SChen Yu 		fprintf(outf, "Kernel command line: %s", bootcmd);
624303331233SChen Yu 	}
624403331233SChen Yu 
624503331233SChen Yu 	fclose(fp);
624603331233SChen Yu }
624703331233SChen Yu 
6248495c7654SLen Brown int add_counter(unsigned int msr_num, char *path, char *name,
6249495c7654SLen Brown 		unsigned int width, enum counter_scope scope,
625041618e63SLen Brown 		enum counter_type type, enum counter_format format, int flags)
6251388e9c81SLen Brown {
6252388e9c81SLen Brown 	struct msr_counter *msrp;
6253388e9c81SLen Brown 
6254388e9c81SLen Brown 	msrp = calloc(1, sizeof(struct msr_counter));
6255388e9c81SLen Brown 	if (msrp == NULL) {
6256388e9c81SLen Brown 		perror("calloc");
6257388e9c81SLen Brown 		exit(1);
6258388e9c81SLen Brown 	}
6259388e9c81SLen Brown 
6260388e9c81SLen Brown 	msrp->msr_num = msr_num;
6261d8d005baSLen Brown 	strncpy(msrp->name, name, NAME_BYTES - 1);
6262495c7654SLen Brown 	if (path)
6263d8d005baSLen Brown 		strncpy(msrp->path, path, PATH_BYTES - 1);
6264388e9c81SLen Brown 	msrp->width = width;
6265388e9c81SLen Brown 	msrp->type = type;
6266388e9c81SLen Brown 	msrp->format = format;
626741618e63SLen Brown 	msrp->flags = flags;
6268388e9c81SLen Brown 
6269388e9c81SLen Brown 	switch (scope) {
6270388e9c81SLen Brown 
6271388e9c81SLen Brown 	case SCOPE_CPU:
6272388e9c81SLen Brown 		msrp->next = sys.tp;
6273388e9c81SLen Brown 		sys.tp = msrp;
6274678a3bd1SLen Brown 		sys.added_thread_counters++;
62750748eaf0SLen Brown 		if (sys.added_thread_counters > MAX_ADDED_THREAD_COUNTERS) {
62761b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added thread counters\n", MAX_ADDED_COUNTERS);
6277678a3bd1SLen Brown 			exit(-1);
6278678a3bd1SLen Brown 		}
6279388e9c81SLen Brown 		break;
6280388e9c81SLen Brown 
6281388e9c81SLen Brown 	case SCOPE_CORE:
6282388e9c81SLen Brown 		msrp->next = sys.cp;
6283388e9c81SLen Brown 		sys.cp = msrp;
6284678a3bd1SLen Brown 		sys.added_core_counters++;
6285678a3bd1SLen Brown 		if (sys.added_core_counters > MAX_ADDED_COUNTERS) {
62861b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added core counters\n", MAX_ADDED_COUNTERS);
6287678a3bd1SLen Brown 			exit(-1);
6288678a3bd1SLen Brown 		}
6289388e9c81SLen Brown 		break;
6290388e9c81SLen Brown 
6291388e9c81SLen Brown 	case SCOPE_PACKAGE:
6292388e9c81SLen Brown 		msrp->next = sys.pp;
6293388e9c81SLen Brown 		sys.pp = msrp;
6294678a3bd1SLen Brown 		sys.added_package_counters++;
6295678a3bd1SLen Brown 		if (sys.added_package_counters > MAX_ADDED_COUNTERS) {
62961b439f01SLen Brown 			fprintf(stderr, "exceeded max %d added package counters\n", MAX_ADDED_COUNTERS);
6297678a3bd1SLen Brown 			exit(-1);
6298678a3bd1SLen Brown 		}
6299388e9c81SLen Brown 		break;
6300388e9c81SLen Brown 	}
6301388e9c81SLen Brown 
6302388e9c81SLen Brown 	return 0;
6303388e9c81SLen Brown }
6304388e9c81SLen Brown 
6305388e9c81SLen Brown void parse_add_command(char *add_command)
6306388e9c81SLen Brown {
6307388e9c81SLen Brown 	int msr_num = 0;
6308495c7654SLen Brown 	char *path = NULL;
63090f47c08dSLen Brown 	char name_buffer[NAME_BYTES] = "";
6310388e9c81SLen Brown 	int width = 64;
6311388e9c81SLen Brown 	int fail = 0;
6312388e9c81SLen Brown 	enum counter_scope scope = SCOPE_CPU;
6313388e9c81SLen Brown 	enum counter_type type = COUNTER_CYCLES;
6314388e9c81SLen Brown 	enum counter_format format = FORMAT_DELTA;
6315388e9c81SLen Brown 
6316388e9c81SLen Brown 	while (add_command) {
6317388e9c81SLen Brown 
6318388e9c81SLen Brown 		if (sscanf(add_command, "msr0x%x", &msr_num) == 1)
6319388e9c81SLen Brown 			goto next;
6320388e9c81SLen Brown 
6321388e9c81SLen Brown 		if (sscanf(add_command, "msr%d", &msr_num) == 1)
6322388e9c81SLen Brown 			goto next;
6323388e9c81SLen Brown 
6324495c7654SLen Brown 		if (*add_command == '/') {
6325495c7654SLen Brown 			path = add_command;
6326495c7654SLen Brown 			goto next;
6327495c7654SLen Brown 		}
6328495c7654SLen Brown 
6329388e9c81SLen Brown 		if (sscanf(add_command, "u%d", &width) == 1) {
6330388e9c81SLen Brown 			if ((width == 32) || (width == 64))
6331388e9c81SLen Brown 				goto next;
6332388e9c81SLen Brown 			width = 64;
6333388e9c81SLen Brown 		}
6334388e9c81SLen Brown 		if (!strncmp(add_command, "cpu", strlen("cpu"))) {
6335388e9c81SLen Brown 			scope = SCOPE_CPU;
6336388e9c81SLen Brown 			goto next;
6337388e9c81SLen Brown 		}
6338388e9c81SLen Brown 		if (!strncmp(add_command, "core", strlen("core"))) {
6339388e9c81SLen Brown 			scope = SCOPE_CORE;
6340388e9c81SLen Brown 			goto next;
6341388e9c81SLen Brown 		}
6342388e9c81SLen Brown 		if (!strncmp(add_command, "package", strlen("package"))) {
6343388e9c81SLen Brown 			scope = SCOPE_PACKAGE;
6344388e9c81SLen Brown 			goto next;
6345388e9c81SLen Brown 		}
6346388e9c81SLen Brown 		if (!strncmp(add_command, "cycles", strlen("cycles"))) {
6347388e9c81SLen Brown 			type = COUNTER_CYCLES;
6348388e9c81SLen Brown 			goto next;
6349388e9c81SLen Brown 		}
6350388e9c81SLen Brown 		if (!strncmp(add_command, "seconds", strlen("seconds"))) {
6351388e9c81SLen Brown 			type = COUNTER_SECONDS;
6352388e9c81SLen Brown 			goto next;
6353388e9c81SLen Brown 		}
635441618e63SLen Brown 		if (!strncmp(add_command, "usec", strlen("usec"))) {
635541618e63SLen Brown 			type = COUNTER_USEC;
635641618e63SLen Brown 			goto next;
635741618e63SLen Brown 		}
6358388e9c81SLen Brown 		if (!strncmp(add_command, "raw", strlen("raw"))) {
6359388e9c81SLen Brown 			format = FORMAT_RAW;
6360388e9c81SLen Brown 			goto next;
6361388e9c81SLen Brown 		}
6362388e9c81SLen Brown 		if (!strncmp(add_command, "delta", strlen("delta"))) {
6363388e9c81SLen Brown 			format = FORMAT_DELTA;
6364388e9c81SLen Brown 			goto next;
6365388e9c81SLen Brown 		}
6366388e9c81SLen Brown 		if (!strncmp(add_command, "percent", strlen("percent"))) {
6367388e9c81SLen Brown 			format = FORMAT_PERCENT;
6368388e9c81SLen Brown 			goto next;
6369388e9c81SLen Brown 		}
6370388e9c81SLen Brown 
6371388e9c81SLen Brown 		if (sscanf(add_command, "%18s,%*s", name_buffer) == 1) {	/* 18 < NAME_BYTES */
6372388e9c81SLen Brown 			char *eos;
6373388e9c81SLen Brown 
6374388e9c81SLen Brown 			eos = strchr(name_buffer, ',');
6375388e9c81SLen Brown 			if (eos)
6376388e9c81SLen Brown 				*eos = '\0';
6377388e9c81SLen Brown 			goto next;
6378388e9c81SLen Brown 		}
6379388e9c81SLen Brown 
6380388e9c81SLen Brown next:
6381388e9c81SLen Brown 		add_command = strchr(add_command, ',');
6382495c7654SLen Brown 		if (add_command) {
6383495c7654SLen Brown 			*add_command = '\0';
6384388e9c81SLen Brown 			add_command++;
6385495c7654SLen Brown 		}
6386388e9c81SLen Brown 
6387388e9c81SLen Brown 	}
6388495c7654SLen Brown 	if ((msr_num == 0) && (path == NULL)) {
6389495c7654SLen Brown 		fprintf(stderr, "--add: (msrDDD | msr0xXXX | /path_to_counter ) required\n");
6390388e9c81SLen Brown 		fail++;
6391388e9c81SLen Brown 	}
6392388e9c81SLen Brown 
6393388e9c81SLen Brown 	/* generate default column header */
6394388e9c81SLen Brown 	if (*name_buffer == '\0') {
6395388e9c81SLen Brown 		if (width == 32)
63965f3aea57SLen Brown 			sprintf(name_buffer, "M0x%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6397388e9c81SLen Brown 		else
63985f3aea57SLen Brown 			sprintf(name_buffer, "M0X%x%s", msr_num, format == FORMAT_PERCENT ? "%" : "");
6399388e9c81SLen Brown 	}
6400388e9c81SLen Brown 
640141618e63SLen Brown 	if (add_counter(msr_num, path, name_buffer, width, scope, type, format, 0))
6402388e9c81SLen Brown 		fail++;
6403388e9c81SLen Brown 
6404388e9c81SLen Brown 	if (fail) {
6405388e9c81SLen Brown 		help();
6406388e9c81SLen Brown 		exit(1);
6407388e9c81SLen Brown 	}
6408388e9c81SLen Brown }
640941618e63SLen Brown 
64100fc521bcSZephaniah E. Loss-Cutler-Hull int is_deferred_add(char *name)
64110fc521bcSZephaniah E. Loss-Cutler-Hull {
64120fc521bcSZephaniah E. Loss-Cutler-Hull 	int i;
64130fc521bcSZephaniah E. Loss-Cutler-Hull 
64140fc521bcSZephaniah E. Loss-Cutler-Hull 	for (i = 0; i < deferred_add_index; ++i)
64150fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!strcmp(name, deferred_add_names[i]))
64160fc521bcSZephaniah E. Loss-Cutler-Hull 			return 1;
64170fc521bcSZephaniah E. Loss-Cutler-Hull 	return 0;
64180fc521bcSZephaniah E. Loss-Cutler-Hull }
64190fc521bcSZephaniah E. Loss-Cutler-Hull 
6420dd778a5eSLen Brown int is_deferred_skip(char *name)
6421dd778a5eSLen Brown {
6422dd778a5eSLen Brown 	int i;
6423dd778a5eSLen Brown 
6424dd778a5eSLen Brown 	for (i = 0; i < deferred_skip_index; ++i)
6425dd778a5eSLen Brown 		if (!strcmp(name, deferred_skip_names[i]))
6426dd778a5eSLen Brown 			return 1;
6427dd778a5eSLen Brown 	return 0;
6428dd778a5eSLen Brown }
6429dd778a5eSLen Brown 
643041618e63SLen Brown void probe_sysfs(void)
643141618e63SLen Brown {
643241618e63SLen Brown 	char path[64];
643341618e63SLen Brown 	char name_buf[16];
643441618e63SLen Brown 	FILE *input;
643541618e63SLen Brown 	int state;
643641618e63SLen Brown 	char *sp;
643741618e63SLen Brown 
64380748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
643941618e63SLen Brown 
64401b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
644141618e63SLen Brown 		input = fopen(path, "r");
644241618e63SLen Brown 		if (input == NULL)
644341618e63SLen Brown 			continue;
64448173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
64458173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
644641618e63SLen Brown 
644741618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
644841618e63SLen Brown 		sp = strchr(name_buf, '-');
644941618e63SLen Brown 		if (!sp)
645041618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
645141618e63SLen Brown 		*sp = '%';
645241618e63SLen Brown 		*(sp + 1) = '\0';
645341618e63SLen Brown 
6454fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
6455fecb3bc8SDavid Arcari 
645641618e63SLen Brown 		fclose(input);
645741618e63SLen Brown 
645841618e63SLen Brown 		sprintf(path, "cpuidle/state%d/time", state);
645941618e63SLen Brown 
64600fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
64610fc521bcSZephaniah E. Loss-Cutler-Hull 			continue;
64620fc521bcSZephaniah E. Loss-Cutler-Hull 
6463dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
6464dd778a5eSLen Brown 			continue;
6465dd778a5eSLen Brown 
64661b439f01SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_USEC, FORMAT_PERCENT, SYSFS_PERCPU);
646741618e63SLen Brown 	}
646841618e63SLen Brown 
64690748eaf0SLen Brown 	for (state = 10; state >= 0; --state) {
647041618e63SLen Brown 
64711b439f01SLen Brown 		sprintf(path, "/sys/devices/system/cpu/cpu%d/cpuidle/state%d/name", base_cpu, state);
647241618e63SLen Brown 		input = fopen(path, "r");
647341618e63SLen Brown 		if (input == NULL)
647441618e63SLen Brown 			continue;
64758173c336SBen Hutchings 		if (!fgets(name_buf, sizeof(name_buf), input))
64768173c336SBen Hutchings 			err(1, "%s: failed to read file", path);
647741618e63SLen Brown 		/* truncate "C1-HSW\n" to "C1", or truncate "C1\n" to "C1" */
647841618e63SLen Brown 		sp = strchr(name_buf, '-');
647941618e63SLen Brown 		if (!sp)
648041618e63SLen Brown 			sp = strchrnul(name_buf, '\n');
648141618e63SLen Brown 		*sp = '\0';
648241618e63SLen Brown 		fclose(input);
648341618e63SLen Brown 
6484fecb3bc8SDavid Arcari 		remove_underbar(name_buf);
6485fecb3bc8SDavid Arcari 
648641618e63SLen Brown 		sprintf(path, "cpuidle/state%d/usage", state);
648741618e63SLen Brown 
64880fc521bcSZephaniah E. Loss-Cutler-Hull 		if (!DO_BIC(BIC_sysfs) && !is_deferred_add(name_buf))
64890fc521bcSZephaniah E. Loss-Cutler-Hull 			continue;
64900fc521bcSZephaniah E. Loss-Cutler-Hull 
6491dd778a5eSLen Brown 		if (is_deferred_skip(name_buf))
6492dd778a5eSLen Brown 			continue;
6493dd778a5eSLen Brown 
64941b439f01SLen Brown 		add_counter(0, path, name_buf, 64, SCOPE_CPU, COUNTER_ITEMS, FORMAT_DELTA, SYSFS_PERCPU);
649541618e63SLen Brown 	}
649641618e63SLen Brown 
649741618e63SLen Brown }
649841618e63SLen Brown 
64991ef7d21aSLen Brown /*
65001ef7d21aSLen Brown  * parse cpuset with following syntax
65011ef7d21aSLen Brown  * 1,2,4..6,8-10 and set bits in cpu_subset
65021ef7d21aSLen Brown  */
65031ef7d21aSLen Brown void parse_cpu_command(char *optarg)
65041ef7d21aSLen Brown {
65051ef7d21aSLen Brown 	unsigned int start, end;
65061ef7d21aSLen Brown 	char *next;
65071ef7d21aSLen Brown 
65084e4e1e7cSLen Brown 	if (!strcmp(optarg, "core")) {
65094e4e1e7cSLen Brown 		if (cpu_subset)
65104e4e1e7cSLen Brown 			goto error;
65114e4e1e7cSLen Brown 		show_core_only++;
65124e4e1e7cSLen Brown 		return;
65134e4e1e7cSLen Brown 	}
65144e4e1e7cSLen Brown 	if (!strcmp(optarg, "package")) {
65154e4e1e7cSLen Brown 		if (cpu_subset)
65164e4e1e7cSLen Brown 			goto error;
65174e4e1e7cSLen Brown 		show_pkg_only++;
65184e4e1e7cSLen Brown 		return;
65194e4e1e7cSLen Brown 	}
65204e4e1e7cSLen Brown 	if (show_core_only || show_pkg_only)
65214e4e1e7cSLen Brown 		goto error;
65224e4e1e7cSLen Brown 
65231ef7d21aSLen Brown 	cpu_subset = CPU_ALLOC(CPU_SUBSET_MAXCPUS);
65241ef7d21aSLen Brown 	if (cpu_subset == NULL)
65251ef7d21aSLen Brown 		err(3, "CPU_ALLOC");
65261ef7d21aSLen Brown 	cpu_subset_size = CPU_ALLOC_SIZE(CPU_SUBSET_MAXCPUS);
65271ef7d21aSLen Brown 
65281ef7d21aSLen Brown 	CPU_ZERO_S(cpu_subset_size, cpu_subset);
65291ef7d21aSLen Brown 
65301ef7d21aSLen Brown 	next = optarg;
65311ef7d21aSLen Brown 
65321ef7d21aSLen Brown 	while (next && *next) {
65331ef7d21aSLen Brown 
65341ef7d21aSLen Brown 		if (*next == '-')	/* no negative cpu numbers */
65351ef7d21aSLen Brown 			goto error;
65361ef7d21aSLen Brown 
65371ef7d21aSLen Brown 		start = strtoul(next, &next, 10);
65381ef7d21aSLen Brown 
65391ef7d21aSLen Brown 		if (start >= CPU_SUBSET_MAXCPUS)
65401ef7d21aSLen Brown 			goto error;
65411ef7d21aSLen Brown 		CPU_SET_S(start, cpu_subset_size, cpu_subset);
65421ef7d21aSLen Brown 
65431ef7d21aSLen Brown 		if (*next == '\0')
65441ef7d21aSLen Brown 			break;
65451ef7d21aSLen Brown 
65461ef7d21aSLen Brown 		if (*next == ',') {
65471ef7d21aSLen Brown 			next += 1;
65481ef7d21aSLen Brown 			continue;
65491ef7d21aSLen Brown 		}
65501ef7d21aSLen Brown 
65511ef7d21aSLen Brown 		if (*next == '-') {
65521ef7d21aSLen Brown 			next += 1;	/* start range */
65531ef7d21aSLen Brown 		} else if (*next == '.') {
65541ef7d21aSLen Brown 			next += 1;
65551ef7d21aSLen Brown 			if (*next == '.')
65561ef7d21aSLen Brown 				next += 1;	/* start range */
65571ef7d21aSLen Brown 			else
65581ef7d21aSLen Brown 				goto error;
65591ef7d21aSLen Brown 		}
65601ef7d21aSLen Brown 
65611ef7d21aSLen Brown 		end = strtoul(next, &next, 10);
65621ef7d21aSLen Brown 		if (end <= start)
65631ef7d21aSLen Brown 			goto error;
65641ef7d21aSLen Brown 
65651ef7d21aSLen Brown 		while (++start <= end) {
65661ef7d21aSLen Brown 			if (start >= CPU_SUBSET_MAXCPUS)
65671ef7d21aSLen Brown 				goto error;
65681ef7d21aSLen Brown 			CPU_SET_S(start, cpu_subset_size, cpu_subset);
65691ef7d21aSLen Brown 		}
65701ef7d21aSLen Brown 
65711ef7d21aSLen Brown 		if (*next == ',')
65721ef7d21aSLen Brown 			next += 1;
65731ef7d21aSLen Brown 		else if (*next != '\0')
65741ef7d21aSLen Brown 			goto error;
65751ef7d21aSLen Brown 	}
65761ef7d21aSLen Brown 
65771ef7d21aSLen Brown 	return;
65781ef7d21aSLen Brown 
65791ef7d21aSLen Brown error:
65804e4e1e7cSLen Brown 	fprintf(stderr, "\"--cpu %s\" malformed\n", optarg);
65814e4e1e7cSLen Brown 	help();
65821ef7d21aSLen Brown 	exit(-1);
65831ef7d21aSLen Brown }
65841ef7d21aSLen Brown 
6585103a8feaSLen Brown void cmdline(int argc, char **argv)
6586103a8feaSLen Brown {
6587103a8feaSLen Brown 	int opt;
6588d8af6f5fSLen Brown 	int option_index = 0;
6589d8af6f5fSLen Brown 	static struct option long_options[] = {
6590388e9c81SLen Brown 		{ "add", required_argument, 0, 'a' },
65911ef7d21aSLen Brown 		{ "cpu", required_argument, 0, 'c' },
6592d8af6f5fSLen Brown 		{ "Dump", no_argument, 0, 'D' },
659396e47158SLen Brown 		{ "debug", no_argument, 0, 'd' },	/* internal, not documented */
65943f44a5c6SLen Brown 		{ "enable", required_argument, 0, 'e' },
6595d8af6f5fSLen Brown 		{ "interval", required_argument, 0, 'i' },
65962af4f9b8SLen Brown 		{ "IPC", no_argument, 0, 'I' },
6597023fe0acSChen Yu 		{ "num_iterations", required_argument, 0, 'n' },
6598c7e399f8SZephaniah E. Loss-Cutler-Hull 		{ "header_iterations", required_argument, 0, 'N' },
6599d8af6f5fSLen Brown 		{ "help", no_argument, 0, 'h' },
6600812db3f7SLen Brown 		{ "hide", required_argument, 0, 'H' },	// meh, -h taken by --help
6601d8af6f5fSLen Brown 		{ "Joules", no_argument, 0, 'J' },
6602c8ade361SLen Brown 		{ "list", no_argument, 0, 'l' },
6603b7d8c148SLen Brown 		{ "out", required_argument, 0, 'o' },
660496e47158SLen Brown 		{ "quiet", no_argument, 0, 'q' },
6605812db3f7SLen Brown 		{ "show", required_argument, 0, 's' },
6606d8af6f5fSLen Brown 		{ "Summary", no_argument, 0, 'S' },
6607d8af6f5fSLen Brown 		{ "TCC", required_argument, 0, 'T' },
6608d8af6f5fSLen Brown 		{ "version", no_argument, 0, 'v' },
6609d8af6f5fSLen Brown 		{ 0, 0, 0, 0 }
6610d8af6f5fSLen Brown 	};
6611103a8feaSLen Brown 
6612103a8feaSLen Brown 	progname = argv[0];
6613103a8feaSLen Brown 
66141b439f01SLen Brown 	while ((opt = getopt_long_only(argc, argv, "+C:c:Dde:hi:Jn:o:qST:v", long_options, &option_index)) != -1) {
6615103a8feaSLen Brown 		switch (opt) {
6616388e9c81SLen Brown 		case 'a':
6617388e9c81SLen Brown 			parse_add_command(optarg);
6618388e9c81SLen Brown 			break;
66191ef7d21aSLen Brown 		case 'c':
66201ef7d21aSLen Brown 			parse_cpu_command(optarg);
66211ef7d21aSLen Brown 			break;
6622d8af6f5fSLen Brown 		case 'D':
6623d8af6f5fSLen Brown 			dump_only++;
66248e180f3cSLen Brown 			break;
66253f44a5c6SLen Brown 		case 'e':
66263f44a5c6SLen Brown 			/* --enable specified counter */
66274c2122d4SLen Brown 			bic_enabled = bic_enabled | bic_lookup(optarg, SHOW_LIST);
66283f44a5c6SLen Brown 			break;
6629d8af6f5fSLen Brown 		case 'd':
6630d8af6f5fSLen Brown 			debug++;
66313f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
66322f32edf1SLen Brown 			break;
6633812db3f7SLen Brown 		case 'H':
66343f44a5c6SLen Brown 			/*
66353f44a5c6SLen Brown 			 * --hide: do not show those specified
66363f44a5c6SLen Brown 			 *  multiple invocations simply clear more bits in enabled mask
66373f44a5c6SLen Brown 			 */
66383f44a5c6SLen Brown 			bic_enabled &= ~bic_lookup(optarg, HIDE_LIST);
6639812db3f7SLen Brown 			break;
6640d8af6f5fSLen Brown 		case 'h':
6641d8af6f5fSLen Brown 		default:
6642d8af6f5fSLen Brown 			help();
6643d8af6f5fSLen Brown 			exit(1);
6644d8af6f5fSLen Brown 		case 'i':
66452a0609c0SLen Brown 			{
66462a0609c0SLen Brown 				double interval = strtod(optarg, NULL);
66472a0609c0SLen Brown 
66482a0609c0SLen Brown 				if (interval < 0.001) {
66491b439f01SLen Brown 					fprintf(outf, "interval %f seconds is too small\n", interval);
66502a0609c0SLen Brown 					exit(2);
66512a0609c0SLen Brown 				}
66522a0609c0SLen Brown 
665347936f94SArtem Bityutskiy 				interval_tv.tv_sec = interval_ts.tv_sec = interval;
6654b9ad8ee0SLen Brown 				interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
665547936f94SArtem Bityutskiy 				interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
66562a0609c0SLen Brown 			}
6657889facbeSLen Brown 			break;
66585c56be9aSDirk Brandewie 		case 'J':
66595c56be9aSDirk Brandewie 			rapl_joules++;
66605c56be9aSDirk Brandewie 			break;
6661c8ade361SLen Brown 		case 'l':
66623f44a5c6SLen Brown 			ENABLE_BIC(BIC_DISABLED_BY_DEFAULT);
6663c8ade361SLen Brown 			list_header_only++;
6664c8ade361SLen Brown 			quiet++;
6665c8ade361SLen Brown 			break;
6666b7d8c148SLen Brown 		case 'o':
6667b7d8c148SLen Brown 			outf = fopen_or_die(optarg, "w");
6668b7d8c148SLen Brown 			break;
666996e47158SLen Brown 		case 'q':
667096e47158SLen Brown 			quiet = 1;
667196e47158SLen Brown 			break;
6672023fe0acSChen Yu 		case 'n':
6673023fe0acSChen Yu 			num_iterations = strtod(optarg, NULL);
6674023fe0acSChen Yu 
6675023fe0acSChen Yu 			if (num_iterations <= 0) {
66761b439f01SLen Brown 				fprintf(outf, "iterations %d should be positive number\n", num_iterations);
6677023fe0acSChen Yu 				exit(2);
6678023fe0acSChen Yu 			}
6679023fe0acSChen Yu 			break;
6680c7e399f8SZephaniah E. Loss-Cutler-Hull 		case 'N':
6681c7e399f8SZephaniah E. Loss-Cutler-Hull 			header_iterations = strtod(optarg, NULL);
6682c7e399f8SZephaniah E. Loss-Cutler-Hull 
6683c7e399f8SZephaniah E. Loss-Cutler-Hull 			if (header_iterations <= 0) {
6684164d7a96SLen Brown 				fprintf(outf, "iterations %d should be positive number\n", header_iterations);
6685c7e399f8SZephaniah E. Loss-Cutler-Hull 				exit(2);
6686c7e399f8SZephaniah E. Loss-Cutler-Hull 			}
6687c7e399f8SZephaniah E. Loss-Cutler-Hull 			break;
6688812db3f7SLen Brown 		case 's':
66893f44a5c6SLen Brown 			/*
66903f44a5c6SLen Brown 			 * --show: show only those specified
66913f44a5c6SLen Brown 			 *  The 1st invocation will clear and replace the enabled mask
66923f44a5c6SLen Brown 			 *  subsequent invocations can add to it.
66933f44a5c6SLen Brown 			 */
66943f44a5c6SLen Brown 			if (shown == 0)
66953f44a5c6SLen Brown 				bic_enabled = bic_lookup(optarg, SHOW_LIST);
66963f44a5c6SLen Brown 			else
66973f44a5c6SLen Brown 				bic_enabled |= bic_lookup(optarg, SHOW_LIST);
66983f44a5c6SLen Brown 			shown = 1;
6699812db3f7SLen Brown 			break;
6700d8af6f5fSLen Brown 		case 'S':
6701d8af6f5fSLen Brown 			summary_only++;
6702d8af6f5fSLen Brown 			break;
6703d8af6f5fSLen Brown 		case 'T':
670455279aefSZhang Rui 			tj_max_override = atoi(optarg);
6705d8af6f5fSLen Brown 			break;
6706d8af6f5fSLen Brown 		case 'v':
6707d8af6f5fSLen Brown 			print_version();
6708d8af6f5fSLen Brown 			exit(0);
6709d8af6f5fSLen Brown 			break;
6710103a8feaSLen Brown 		}
6711103a8feaSLen Brown 	}
6712103a8feaSLen Brown }
6713103a8feaSLen Brown 
6714103a8feaSLen Brown int main(int argc, char **argv)
6715103a8feaSLen Brown {
6716b7d8c148SLen Brown 	outf = stderr;
6717103a8feaSLen Brown 	cmdline(argc, argv);
6718103a8feaSLen Brown 
671903331233SChen Yu 	if (!quiet) {
6720d8af6f5fSLen Brown 		print_version();
672103331233SChen Yu 		print_bootcmd();
672203331233SChen Yu 	}
6723103a8feaSLen Brown 
672441618e63SLen Brown 	probe_sysfs();
672541618e63SLen Brown 
6726103a8feaSLen Brown 	turbostat_init();
6727103a8feaSLen Brown 
67286799ba84SDan Merillat 	msr_sum_record();
67296799ba84SDan Merillat 
67303b4d5c7fSAndy Shevchenko 	/* dump counters and exit */
67313b4d5c7fSAndy Shevchenko 	if (dump_only)
67323b4d5c7fSAndy Shevchenko 		return get_and_dump_counters();
67333b4d5c7fSAndy Shevchenko 
6734c8ade361SLen Brown 	/* list header and exit */
6735c8ade361SLen Brown 	if (list_header_only) {
6736c8ade361SLen Brown 		print_header(",");
6737c8ade361SLen Brown 		flush_output_stdout();
6738c8ade361SLen Brown 		return 0;
6739c8ade361SLen Brown 	}
6740c8ade361SLen Brown 
6741103a8feaSLen Brown 	/*
6742103a8feaSLen Brown 	 * if any params left, it must be a command to fork
6743103a8feaSLen Brown 	 */
6744103a8feaSLen Brown 	if (argc - optind)
6745103a8feaSLen Brown 		return fork_it(argv + optind);
6746103a8feaSLen Brown 	else
6747103a8feaSLen Brown 		turbostat_loop();
6748103a8feaSLen Brown 
6749103a8feaSLen Brown 	return 0;
6750103a8feaSLen Brown }
6751