xref: /linux/tools/power/cpupower/utils/cpufreq-info.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
14f19048fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27fe2f639SDominik Brodowski /*
37fe2f639SDominik Brodowski  *  (C) 2004-2009  Dominik Brodowski <linux@dominikbrodowski.de>
47fe2f639SDominik Brodowski  */
57fe2f639SDominik Brodowski 
67fe2f639SDominik Brodowski 
77fe2f639SDominik Brodowski #include <unistd.h>
87fe2f639SDominik Brodowski #include <stdio.h>
97fe2f639SDominik Brodowski #include <errno.h>
107fe2f639SDominik Brodowski #include <stdlib.h>
117fe2f639SDominik Brodowski #include <string.h>
1238cb76a3SShreyas B. Prabhu #include <limits.h>
137fe2f639SDominik Brodowski 
147fe2f639SDominik Brodowski #include <getopt.h>
157fe2f639SDominik Brodowski 
167fe2f639SDominik Brodowski #include "cpufreq.h"
17ce512b84SThomas Renninger #include "helpers/sysfs.h"
187fe2f639SDominik Brodowski #include "helpers/helpers.h"
197fe2f639SDominik Brodowski #include "helpers/bitmask.h"
207fe2f639SDominik Brodowski 
217fe2f639SDominik Brodowski #define LINE_LEN 10
227fe2f639SDominik Brodowski 
count_cpus(void)237fe2f639SDominik Brodowski static unsigned int count_cpus(void)
247fe2f639SDominik Brodowski {
257fe2f639SDominik Brodowski 	FILE *fp;
267fe2f639SDominik Brodowski 	char value[LINE_LEN];
277fe2f639SDominik Brodowski 	unsigned int ret = 0;
287fe2f639SDominik Brodowski 	unsigned int cpunr = 0;
297fe2f639SDominik Brodowski 
307fe2f639SDominik Brodowski 	fp = fopen("/proc/stat", "r");
317fe2f639SDominik Brodowski 	if (!fp) {
327fe2f639SDominik Brodowski 		printf(_("Couldn't count the number of CPUs (%s: %s), assuming 1\n"), "/proc/stat", strerror(errno));
337fe2f639SDominik Brodowski 		return 1;
347fe2f639SDominik Brodowski 	}
357fe2f639SDominik Brodowski 
367fe2f639SDominik Brodowski 	while (!feof(fp)) {
377fe2f639SDominik Brodowski 		if (!fgets(value, LINE_LEN, fp))
387fe2f639SDominik Brodowski 			continue;
397fe2f639SDominik Brodowski 		value[LINE_LEN - 1] = '\0';
407fe2f639SDominik Brodowski 		if (strlen(value) < (LINE_LEN - 2))
417fe2f639SDominik Brodowski 			continue;
427fe2f639SDominik Brodowski 		if (strstr(value, "cpu "))
437fe2f639SDominik Brodowski 			continue;
447fe2f639SDominik Brodowski 		if (sscanf(value, "cpu%d ", &cpunr) != 1)
457fe2f639SDominik Brodowski 			continue;
467fe2f639SDominik Brodowski 		if (cpunr > ret)
477fe2f639SDominik Brodowski 			ret = cpunr;
487fe2f639SDominik Brodowski 	}
497fe2f639SDominik Brodowski 	fclose(fp);
507fe2f639SDominik Brodowski 
517fe2f639SDominik Brodowski 	/* cpu count starts from 0, on error return 1 (UP) */
52a1ce5ba2SDominik Brodowski 	return ret + 1;
537fe2f639SDominik Brodowski }
547fe2f639SDominik Brodowski 
557fe2f639SDominik Brodowski 
proc_cpufreq_output(void)567fe2f639SDominik Brodowski static void proc_cpufreq_output(void)
577fe2f639SDominik Brodowski {
587fe2f639SDominik Brodowski 	unsigned int cpu, nr_cpus;
597fe2f639SDominik Brodowski 	struct cpufreq_policy *policy;
607fe2f639SDominik Brodowski 	unsigned int min_pctg = 0;
617fe2f639SDominik Brodowski 	unsigned int max_pctg = 0;
627fe2f639SDominik Brodowski 	unsigned long min, max;
637fe2f639SDominik Brodowski 
647fe2f639SDominik Brodowski 	printf(_("          minimum CPU frequency  -  maximum CPU frequency  -  governor\n"));
657fe2f639SDominik Brodowski 
667fe2f639SDominik Brodowski 	nr_cpus = count_cpus();
677fe2f639SDominik Brodowski 	for (cpu = 0; cpu < nr_cpus; cpu++) {
687fe2f639SDominik Brodowski 		policy = cpufreq_get_policy(cpu);
697fe2f639SDominik Brodowski 		if (!policy)
707fe2f639SDominik Brodowski 			continue;
717fe2f639SDominik Brodowski 
727fe2f639SDominik Brodowski 		if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
737fe2f639SDominik Brodowski 			max = 0;
747fe2f639SDominik Brodowski 		} else {
757fe2f639SDominik Brodowski 			min_pctg = (policy->min * 100) / max;
767fe2f639SDominik Brodowski 			max_pctg = (policy->max * 100) / max;
777fe2f639SDominik Brodowski 		}
787fe2f639SDominik Brodowski 		printf("CPU%3d    %9lu kHz (%3d %%)  -  %9lu kHz (%3d %%)  -  %s\n",
79a1ce5ba2SDominik Brodowski 			cpu , policy->min, max ? min_pctg : 0, policy->max,
80a1ce5ba2SDominik Brodowski 			max ? max_pctg : 0, policy->governor);
817fe2f639SDominik Brodowski 
827fe2f639SDominik Brodowski 		cpufreq_put_policy(policy);
837fe2f639SDominik Brodowski 	}
847fe2f639SDominik Brodowski }
857fe2f639SDominik Brodowski 
86e091abc7SPrarit Bhargava static int no_rounding;
print_duration(unsigned long duration)877fe2f639SDominik Brodowski static void print_duration(unsigned long duration)
887fe2f639SDominik Brodowski {
897fe2f639SDominik Brodowski 	unsigned long tmp;
907fe2f639SDominik Brodowski 
91e091abc7SPrarit Bhargava 	if (no_rounding) {
92e091abc7SPrarit Bhargava 		if (duration > 1000000)
93e091abc7SPrarit Bhargava 			printf("%u.%06u ms", ((unsigned int) duration/1000000),
94e091abc7SPrarit Bhargava 				((unsigned int) duration%1000000));
95e091abc7SPrarit Bhargava 		else if (duration > 100000)
96e091abc7SPrarit Bhargava 			printf("%u us", ((unsigned int) duration/1000));
97e091abc7SPrarit Bhargava 		else if (duration > 1000)
98e091abc7SPrarit Bhargava 			printf("%u.%03u us", ((unsigned int) duration/1000),
99e091abc7SPrarit Bhargava 				((unsigned int) duration%1000));
100e091abc7SPrarit Bhargava 		else
101e091abc7SPrarit Bhargava 			printf("%lu ns", duration);
102e091abc7SPrarit Bhargava 	} else {
1037fe2f639SDominik Brodowski 		if (duration > 1000000) {
1047fe2f639SDominik Brodowski 			tmp = duration%10000;
1057fe2f639SDominik Brodowski 			if (tmp >= 5000)
1067fe2f639SDominik Brodowski 				duration += 10000;
1077fe2f639SDominik Brodowski 			printf("%u.%02u ms", ((unsigned int) duration/1000000),
1087fe2f639SDominik Brodowski 				((unsigned int) (duration%1000000)/10000));
1097fe2f639SDominik Brodowski 		} else if (duration > 100000) {
1107fe2f639SDominik Brodowski 			tmp = duration%1000;
1117fe2f639SDominik Brodowski 			if (tmp >= 500)
1127fe2f639SDominik Brodowski 				duration += 1000;
1137fe2f639SDominik Brodowski 			printf("%u us", ((unsigned int) duration / 1000));
1147fe2f639SDominik Brodowski 		} else if (duration > 1000) {
1157fe2f639SDominik Brodowski 			tmp = duration%100;
1167fe2f639SDominik Brodowski 			if (tmp >= 50)
1177fe2f639SDominik Brodowski 				duration += 100;
1187fe2f639SDominik Brodowski 			printf("%u.%01u us", ((unsigned int) duration/1000),
1197fe2f639SDominik Brodowski 				((unsigned int) (duration%1000)/100));
1207fe2f639SDominik Brodowski 		} else
1217fe2f639SDominik Brodowski 			printf("%lu ns", duration);
122e091abc7SPrarit Bhargava 	}
1237fe2f639SDominik Brodowski 	return;
1247fe2f639SDominik Brodowski }
1257fe2f639SDominik Brodowski 
get_boost_mode_x86(unsigned int cpu)126ae291709SAbhishek Goel static int get_boost_mode_x86(unsigned int cpu)
127a1ce5ba2SDominik Brodowski {
1287fe2f639SDominik Brodowski 	int support, active, b_states = 0, ret, pstate_no, i;
1297fe2f639SDominik Brodowski 	/* ToDo: Make this more global */
1307fe2f639SDominik Brodowski 	unsigned long pstates[MAX_HW_PSTATES] = {0,};
1317fe2f639SDominik Brodowski 
1327fe2f639SDominik Brodowski 	ret = cpufreq_has_boost_support(cpu, &support, &active, &b_states);
1337fe2f639SDominik Brodowski 	if (ret) {
1347fe2f639SDominik Brodowski 		printf(_("Error while evaluating Boost Capabilities"
1357fe2f639SDominik Brodowski 				" on CPU %d -- are you root?\n"), cpu);
1367fe2f639SDominik Brodowski 		return ret;
1377fe2f639SDominik Brodowski 	}
1387fe2f639SDominik Brodowski 	/* P state changes via MSR are identified via cpuid 80000007
1397fe2f639SDominik Brodowski 	   on Intel and AMD, but we assume boost capable machines can do that
1407fe2f639SDominik Brodowski 	   if (cpuid_eax(0x80000000) >= 0x80000007
1417fe2f639SDominik Brodowski 	   && (cpuid_edx(0x80000007) & (1 << 7)))
1427fe2f639SDominik Brodowski 	*/
1437fe2f639SDominik Brodowski 
1447fe2f639SDominik Brodowski 	printf(_("  boost state support:\n"));
1457fe2f639SDominik Brodowski 
1467fe2f639SDominik Brodowski 	printf(_("    Supported: %s\n"), support ? _("yes") : _("no"));
1477fe2f639SDominik Brodowski 	printf(_("    Active: %s\n"), active ? _("yes") : _("no"));
1487fe2f639SDominik Brodowski 
149d8363e29SHuang Rui 	if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
150d8363e29SHuang Rui 	    cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE) {
151d8363e29SHuang Rui 		return 0;
152d8363e29SHuang Rui 	} else if ((cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
153995d5f64SPu Wen 		    cpupower_cpu_info.family >= 0x10) ||
154995d5f64SPu Wen 		   cpupower_cpu_info.vendor == X86_VENDOR_HYGON) {
155d1abc4e9SNathan Fontenot 		ret = decode_pstates(cpu, b_states, pstates, &pstate_no);
1567fe2f639SDominik Brodowski 		if (ret)
1577fe2f639SDominik Brodowski 			return ret;
1587fe2f639SDominik Brodowski 
1597fe2f639SDominik Brodowski 		printf(_("    Boost States: %d\n"), b_states);
1607fe2f639SDominik Brodowski 		printf(_("    Total States: %d\n"), pstate_no);
1617fe2f639SDominik Brodowski 		for (i = 0; i < pstate_no; i++) {
162f69ffc5dSPrarit Bhargava 			if (!pstates[i])
163f69ffc5dSPrarit Bhargava 				continue;
1647fe2f639SDominik Brodowski 			if (i < b_states)
1658fb2e440SThomas Renninger 				printf(_("    Pstate-Pb%d: %luMHz (boost state)"
1668fb2e440SThomas Renninger 					 "\n"), i, pstates[i]);
1677fe2f639SDominik Brodowski 			else
1687fe2f639SDominik Brodowski 				printf(_("    Pstate-P%d:  %luMHz\n"),
1697fe2f639SDominik Brodowski 				       i - b_states, pstates[i]);
1707fe2f639SDominik Brodowski 		}
1718fb2e440SThomas Renninger 	} else if (cpupower_cpu_info.caps & CPUPOWER_CAP_HAS_TURBO_RATIO) {
1728fb2e440SThomas Renninger 		double bclk;
1738fb2e440SThomas Renninger 		unsigned long long intel_turbo_ratio = 0;
1748fb2e440SThomas Renninger 		unsigned int ratio;
1758fb2e440SThomas Renninger 
1768fb2e440SThomas Renninger 		/* Any way to autodetect this ? */
1778fb2e440SThomas Renninger 		if (cpupower_cpu_info.caps & CPUPOWER_CAP_IS_SNB)
1788fb2e440SThomas Renninger 			bclk = 100.00;
1798fb2e440SThomas Renninger 		else
1808fb2e440SThomas Renninger 			bclk = 133.33;
1818fb2e440SThomas Renninger 		intel_turbo_ratio = msr_intel_get_turbo_ratio(cpu);
1828fb2e440SThomas Renninger 		dprint ("    Ratio: 0x%llx - bclk: %f\n",
1838fb2e440SThomas Renninger 			intel_turbo_ratio, bclk);
1848fb2e440SThomas Renninger 
1858fb2e440SThomas Renninger 		ratio = (intel_turbo_ratio >> 24) & 0xFF;
1868fb2e440SThomas Renninger 		if (ratio)
1878fb2e440SThomas Renninger 			printf(_("    %.0f MHz max turbo 4 active cores\n"),
1888fb2e440SThomas Renninger 			       ratio * bclk);
1898fb2e440SThomas Renninger 
1908fb2e440SThomas Renninger 		ratio = (intel_turbo_ratio >> 16) & 0xFF;
1918fb2e440SThomas Renninger 		if (ratio)
1928fb2e440SThomas Renninger 			printf(_("    %.0f MHz max turbo 3 active cores\n"),
1938fb2e440SThomas Renninger 			       ratio * bclk);
1948fb2e440SThomas Renninger 
1958fb2e440SThomas Renninger 		ratio = (intel_turbo_ratio >> 8) & 0xFF;
1968fb2e440SThomas Renninger 		if (ratio)
1978fb2e440SThomas Renninger 			printf(_("    %.0f MHz max turbo 2 active cores\n"),
1988fb2e440SThomas Renninger 			       ratio * bclk);
1998fb2e440SThomas Renninger 
2008fb2e440SThomas Renninger 		ratio = (intel_turbo_ratio >> 0) & 0xFF;
2018fb2e440SThomas Renninger 		if (ratio)
2028fb2e440SThomas Renninger 			printf(_("    %.0f MHz max turbo 1 active cores\n"),
2038fb2e440SThomas Renninger 			       ratio * bclk);
2048fb2e440SThomas Renninger 	}
2057fe2f639SDominik Brodowski 	return 0;
2067fe2f639SDominik Brodowski }
2077fe2f639SDominik Brodowski 
208ae291709SAbhishek Goel /* --boost / -b */
209ae291709SAbhishek Goel 
get_boost_mode(unsigned int cpu)210ae291709SAbhishek Goel static int get_boost_mode(unsigned int cpu)
211ae291709SAbhishek Goel {
21241ddb7e1SThomas Renninger 	struct cpufreq_available_frequencies *freqs;
213ae291709SAbhishek Goel 
214ae291709SAbhishek Goel 	if (cpupower_cpu_info.vendor == X86_VENDOR_AMD ||
215ae291709SAbhishek Goel 	    cpupower_cpu_info.vendor == X86_VENDOR_HYGON ||
216ae291709SAbhishek Goel 	    cpupower_cpu_info.vendor == X86_VENDOR_INTEL)
217ae291709SAbhishek Goel 		return get_boost_mode_x86(cpu);
218ae291709SAbhishek Goel 
21941ddb7e1SThomas Renninger 	freqs = cpufreq_get_boost_frequencies(cpu);
220ae291709SAbhishek Goel 	if (freqs) {
221ae291709SAbhishek Goel 		printf(_("  boost frequency steps: "));
222ae291709SAbhishek Goel 		while (freqs->next) {
22335fdf42dSHuang Rui 			print_speed(freqs->frequency, no_rounding);
224ae291709SAbhishek Goel 			printf(", ");
225ae291709SAbhishek Goel 			freqs = freqs->next;
226ae291709SAbhishek Goel 		}
22735fdf42dSHuang Rui 		print_speed(freqs->frequency, no_rounding);
228ae291709SAbhishek Goel 		printf("\n");
22941ddb7e1SThomas Renninger 		cpufreq_put_available_frequencies(freqs);
230ae291709SAbhishek Goel 	}
231ae291709SAbhishek Goel 
232ae291709SAbhishek Goel 	return 0;
233ae291709SAbhishek Goel }
234ae291709SAbhishek Goel 
2357fe2f639SDominik Brodowski /* --freq / -f */
2367fe2f639SDominik Brodowski 
get_freq_kernel(unsigned int cpu,unsigned int human)237a1ce5ba2SDominik Brodowski static int get_freq_kernel(unsigned int cpu, unsigned int human)
238a1ce5ba2SDominik Brodowski {
2397fe2f639SDominik Brodowski 	unsigned long freq = cpufreq_get_freq_kernel(cpu);
240562e5f1aSJacob Tanenbaum 	printf(_("  current CPU frequency: "));
241562e5f1aSJacob Tanenbaum 	if (!freq) {
242562e5f1aSJacob Tanenbaum 		printf(_(" Unable to call to kernel\n"));
2437fe2f639SDominik Brodowski 		return -EINVAL;
244562e5f1aSJacob Tanenbaum 	}
2457fe2f639SDominik Brodowski 	if (human) {
24635fdf42dSHuang Rui 		print_speed(freq, no_rounding);
2477fe2f639SDominik Brodowski 	} else
248562e5f1aSJacob Tanenbaum 		printf("%lu", freq);
249562e5f1aSJacob Tanenbaum 	printf(_(" (asserted by call to kernel)\n"));
2507fe2f639SDominik Brodowski 	return 0;
2517fe2f639SDominik Brodowski }
2527fe2f639SDominik Brodowski 
2537fe2f639SDominik Brodowski 
2547fe2f639SDominik Brodowski /* --hwfreq / -w */
2557fe2f639SDominik Brodowski 
get_freq_hardware(unsigned int cpu,unsigned int human)256a1ce5ba2SDominik Brodowski static int get_freq_hardware(unsigned int cpu, unsigned int human)
257a1ce5ba2SDominik Brodowski {
2587fe2f639SDominik Brodowski 	unsigned long freq = cpufreq_get_freq_hardware(cpu);
259562e5f1aSJacob Tanenbaum 	printf(_("  current CPU frequency: "));
260562e5f1aSJacob Tanenbaum 	if (!freq) {
261562e5f1aSJacob Tanenbaum 		printf("Unable to call hardware\n");
2627fe2f639SDominik Brodowski 		return -EINVAL;
263562e5f1aSJacob Tanenbaum 	}
2647fe2f639SDominik Brodowski 	if (human) {
26535fdf42dSHuang Rui 		print_speed(freq, no_rounding);
2667fe2f639SDominik Brodowski 	} else
267562e5f1aSJacob Tanenbaum 		printf("%lu", freq);
268562e5f1aSJacob Tanenbaum 	printf(_(" (asserted by call to hardware)\n"));
2697fe2f639SDominik Brodowski 	return 0;
2707fe2f639SDominik Brodowski }
2717fe2f639SDominik Brodowski 
2727fe2f639SDominik Brodowski /* --hwlimits / -l */
2737fe2f639SDominik Brodowski 
get_hardware_limits(unsigned int cpu,unsigned int human)2744da39cebSStafford Horne static int get_hardware_limits(unsigned int cpu, unsigned int human)
275a1ce5ba2SDominik Brodowski {
2767fe2f639SDominik Brodowski 	unsigned long min, max;
277562e5f1aSJacob Tanenbaum 
278562e5f1aSJacob Tanenbaum 	if (cpufreq_get_hardware_limits(cpu, &min, &max)) {
279562e5f1aSJacob Tanenbaum 		printf(_("Not Available\n"));
2807fe2f639SDominik Brodowski 		return -EINVAL;
281562e5f1aSJacob Tanenbaum 	}
282562e5f1aSJacob Tanenbaum 
2834da39cebSStafford Horne 	if (human) {
2844da39cebSStafford Horne 		printf(_("  hardware limits: "));
28535fdf42dSHuang Rui 		print_speed(min, no_rounding);
286562e5f1aSJacob Tanenbaum 		printf(" - ");
28735fdf42dSHuang Rui 		print_speed(max, no_rounding);
288562e5f1aSJacob Tanenbaum 		printf("\n");
2894da39cebSStafford Horne 	} else {
2904da39cebSStafford Horne 		printf("%lu %lu\n", min, max);
2914da39cebSStafford Horne 	}
2927fe2f639SDominik Brodowski 	return 0;
2937fe2f639SDominik Brodowski }
2947fe2f639SDominik Brodowski 
2957fe2f639SDominik Brodowski /* --driver / -d */
2967fe2f639SDominik Brodowski 
get_driver(unsigned int cpu)297a1ce5ba2SDominik Brodowski static int get_driver(unsigned int cpu)
298a1ce5ba2SDominik Brodowski {
2997fe2f639SDominik Brodowski 	char *driver = cpufreq_get_driver(cpu);
300562e5f1aSJacob Tanenbaum 	if (!driver) {
301562e5f1aSJacob Tanenbaum 		printf(_("  no or unknown cpufreq driver is active on this CPU\n"));
3027fe2f639SDominik Brodowski 		return -EINVAL;
303562e5f1aSJacob Tanenbaum 	}
304562e5f1aSJacob Tanenbaum 	printf("  driver: %s\n", driver);
3057fe2f639SDominik Brodowski 	cpufreq_put_driver(driver);
3067fe2f639SDominik Brodowski 	return 0;
3077fe2f639SDominik Brodowski }
3087fe2f639SDominik Brodowski 
3097fe2f639SDominik Brodowski /* --policy / -p */
3107fe2f639SDominik Brodowski 
get_policy(unsigned int cpu)311a1ce5ba2SDominik Brodowski static int get_policy(unsigned int cpu)
312a1ce5ba2SDominik Brodowski {
3137fe2f639SDominik Brodowski 	struct cpufreq_policy *policy = cpufreq_get_policy(cpu);
314562e5f1aSJacob Tanenbaum 	if (!policy) {
315562e5f1aSJacob Tanenbaum 		printf(_("  Unable to determine current policy\n"));
3167fe2f639SDominik Brodowski 		return -EINVAL;
317562e5f1aSJacob Tanenbaum 	}
318562e5f1aSJacob Tanenbaum 	printf(_("  current policy: frequency should be within "));
31935fdf42dSHuang Rui 	print_speed(policy->min, no_rounding);
320562e5f1aSJacob Tanenbaum 	printf(_(" and "));
32135fdf42dSHuang Rui 	print_speed(policy->max, no_rounding);
322562e5f1aSJacob Tanenbaum 
323562e5f1aSJacob Tanenbaum 	printf(".\n                  ");
324562e5f1aSJacob Tanenbaum 	printf(_("The governor \"%s\" may decide which speed to use\n"
325562e5f1aSJacob Tanenbaum 	       "                  within this range.\n"),
326562e5f1aSJacob Tanenbaum 	       policy->governor);
3277fe2f639SDominik Brodowski 	cpufreq_put_policy(policy);
3287fe2f639SDominik Brodowski 	return 0;
3297fe2f639SDominik Brodowski }
3307fe2f639SDominik Brodowski 
3317fe2f639SDominik Brodowski /* --governors / -g */
3327fe2f639SDominik Brodowski 
get_available_governors(unsigned int cpu)333a1ce5ba2SDominik Brodowski static int get_available_governors(unsigned int cpu)
334a1ce5ba2SDominik Brodowski {
335a1ce5ba2SDominik Brodowski 	struct cpufreq_available_governors *governors =
336a1ce5ba2SDominik Brodowski 		cpufreq_get_available_governors(cpu);
337562e5f1aSJacob Tanenbaum 
338562e5f1aSJacob Tanenbaum 	printf(_("  available cpufreq governors: "));
339562e5f1aSJacob Tanenbaum 	if (!governors) {
340562e5f1aSJacob Tanenbaum 		printf(_("Not Available\n"));
3417fe2f639SDominik Brodowski 		return -EINVAL;
342562e5f1aSJacob Tanenbaum 	}
3437fe2f639SDominik Brodowski 
3447fe2f639SDominik Brodowski 	while (governors->next) {
3457fe2f639SDominik Brodowski 		printf("%s ", governors->governor);
3467fe2f639SDominik Brodowski 		governors = governors->next;
3477fe2f639SDominik Brodowski 	}
3487fe2f639SDominik Brodowski 	printf("%s\n", governors->governor);
3497fe2f639SDominik Brodowski 	cpufreq_put_available_governors(governors);
3507fe2f639SDominik Brodowski 	return 0;
3517fe2f639SDominik Brodowski }
3527fe2f639SDominik Brodowski 
3537fe2f639SDominik Brodowski 
3547fe2f639SDominik Brodowski /* --affected-cpus  / -a */
3557fe2f639SDominik Brodowski 
get_affected_cpus(unsigned int cpu)356a1ce5ba2SDominik Brodowski static int get_affected_cpus(unsigned int cpu)
357a1ce5ba2SDominik Brodowski {
3587fe2f639SDominik Brodowski 	struct cpufreq_affected_cpus *cpus = cpufreq_get_affected_cpus(cpu);
359562e5f1aSJacob Tanenbaum 
360562e5f1aSJacob Tanenbaum 	printf(_("  CPUs which need to have their frequency coordinated by software: "));
361562e5f1aSJacob Tanenbaum 	if (!cpus) {
362562e5f1aSJacob Tanenbaum 		printf(_("Not Available\n"));
3637fe2f639SDominik Brodowski 		return -EINVAL;
364562e5f1aSJacob Tanenbaum 	}
3657fe2f639SDominik Brodowski 
3667fe2f639SDominik Brodowski 	while (cpus->next) {
3677fe2f639SDominik Brodowski 		printf("%d ", cpus->cpu);
3687fe2f639SDominik Brodowski 		cpus = cpus->next;
3697fe2f639SDominik Brodowski 	}
3707fe2f639SDominik Brodowski 	printf("%d\n", cpus->cpu);
3717fe2f639SDominik Brodowski 	cpufreq_put_affected_cpus(cpus);
3727fe2f639SDominik Brodowski 	return 0;
3737fe2f639SDominik Brodowski }
3747fe2f639SDominik Brodowski 
3757fe2f639SDominik Brodowski /* --related-cpus  / -r */
3767fe2f639SDominik Brodowski 
get_related_cpus(unsigned int cpu)377a1ce5ba2SDominik Brodowski static int get_related_cpus(unsigned int cpu)
378a1ce5ba2SDominik Brodowski {
3797fe2f639SDominik Brodowski 	struct cpufreq_affected_cpus *cpus = cpufreq_get_related_cpus(cpu);
380562e5f1aSJacob Tanenbaum 
381562e5f1aSJacob Tanenbaum 	printf(_("  CPUs which run at the same hardware frequency: "));
382562e5f1aSJacob Tanenbaum 	if (!cpus) {
383562e5f1aSJacob Tanenbaum 		printf(_("Not Available\n"));
3847fe2f639SDominik Brodowski 		return -EINVAL;
385562e5f1aSJacob Tanenbaum 	}
3867fe2f639SDominik Brodowski 
3877fe2f639SDominik Brodowski 	while (cpus->next) {
3887fe2f639SDominik Brodowski 		printf("%d ", cpus->cpu);
3897fe2f639SDominik Brodowski 		cpus = cpus->next;
3907fe2f639SDominik Brodowski 	}
3917fe2f639SDominik Brodowski 	printf("%d\n", cpus->cpu);
3927fe2f639SDominik Brodowski 	cpufreq_put_related_cpus(cpus);
3937fe2f639SDominik Brodowski 	return 0;
3947fe2f639SDominik Brodowski }
3957fe2f639SDominik Brodowski 
3967fe2f639SDominik Brodowski /* --stats / -s */
3977fe2f639SDominik Brodowski 
get_freq_stats(unsigned int cpu,unsigned int human)398a1ce5ba2SDominik Brodowski static int get_freq_stats(unsigned int cpu, unsigned int human)
399a1ce5ba2SDominik Brodowski {
4007fe2f639SDominik Brodowski 	unsigned long total_trans = cpufreq_get_transitions(cpu);
4017fe2f639SDominik Brodowski 	unsigned long long total_time;
4027fe2f639SDominik Brodowski 	struct cpufreq_stats *stats = cpufreq_get_stats(cpu, &total_time);
4037fe2f639SDominik Brodowski 	while (stats) {
4047fe2f639SDominik Brodowski 		if (human) {
40535fdf42dSHuang Rui 			print_speed(stats->frequency, no_rounding);
406a1ce5ba2SDominik Brodowski 			printf(":%.2f%%",
407a1ce5ba2SDominik Brodowski 				(100.0 * stats->time_in_state) / total_time);
408a1ce5ba2SDominik Brodowski 		} else
409a1ce5ba2SDominik Brodowski 			printf("%lu:%llu",
410a1ce5ba2SDominik Brodowski 				stats->frequency, stats->time_in_state);
4117fe2f639SDominik Brodowski 		stats = stats->next;
4127fe2f639SDominik Brodowski 		if (stats)
4137fe2f639SDominik Brodowski 			printf(", ");
4147fe2f639SDominik Brodowski 	}
4157fe2f639SDominik Brodowski 	cpufreq_put_stats(stats);
4167fe2f639SDominik Brodowski 	if (total_trans)
4177fe2f639SDominik Brodowski 		printf("  (%lu)\n", total_trans);
4187fe2f639SDominik Brodowski 	return 0;
4197fe2f639SDominik Brodowski }
4207fe2f639SDominik Brodowski 
4217fe2f639SDominik Brodowski /* --latency / -y */
4227fe2f639SDominik Brodowski 
get_latency(unsigned int cpu,unsigned int human)423a1ce5ba2SDominik Brodowski static int get_latency(unsigned int cpu, unsigned int human)
424a1ce5ba2SDominik Brodowski {
4257fe2f639SDominik Brodowski 	unsigned long latency = cpufreq_get_transition_latency(cpu);
426562e5f1aSJacob Tanenbaum 
427562e5f1aSJacob Tanenbaum 	printf(_("  maximum transition latency: "));
428e98f033fSJacob Tanenbaum 	if (!latency || latency == UINT_MAX) {
429e98f033fSJacob Tanenbaum 		printf(_(" Cannot determine or is not supported.\n"));
4307fe2f639SDominik Brodowski 		return -EINVAL;
431562e5f1aSJacob Tanenbaum 	}
4327fe2f639SDominik Brodowski 
4337fe2f639SDominik Brodowski 	if (human) {
4347fe2f639SDominik Brodowski 		print_duration(latency);
4357fe2f639SDominik Brodowski 		printf("\n");
4367fe2f639SDominik Brodowski 	} else
4377fe2f639SDominik Brodowski 		printf("%lu\n", latency);
4387fe2f639SDominik Brodowski 	return 0;
4397fe2f639SDominik Brodowski }
4407fe2f639SDominik Brodowski 
4418382dce5SHuang Rui /* --performance / -c */
4428382dce5SHuang Rui 
get_perf_cap(unsigned int cpu)4438382dce5SHuang Rui static int get_perf_cap(unsigned int cpu)
4448382dce5SHuang Rui {
4458382dce5SHuang Rui 	if (cpupower_cpu_info.vendor == X86_VENDOR_AMD &&
4468382dce5SHuang Rui 	    cpupower_cpu_info.caps & CPUPOWER_CAP_AMD_PSTATE)
4478382dce5SHuang Rui 		amd_pstate_show_perf_and_freq(cpu, no_rounding);
4488382dce5SHuang Rui 
4498382dce5SHuang Rui 	return 0;
4508382dce5SHuang Rui }
4518382dce5SHuang Rui 
debug_output_one(unsigned int cpu)452562e5f1aSJacob Tanenbaum static void debug_output_one(unsigned int cpu)
453562e5f1aSJacob Tanenbaum {
45441ddb7e1SThomas Renninger 	struct cpufreq_available_frequencies *freqs;
455562e5f1aSJacob Tanenbaum 
456562e5f1aSJacob Tanenbaum 	get_driver(cpu);
457562e5f1aSJacob Tanenbaum 	get_related_cpus(cpu);
458562e5f1aSJacob Tanenbaum 	get_affected_cpus(cpu);
459562e5f1aSJacob Tanenbaum 	get_latency(cpu, 1);
4604da39cebSStafford Horne 	get_hardware_limits(cpu, 1);
461562e5f1aSJacob Tanenbaum 
46241ddb7e1SThomas Renninger 	freqs = cpufreq_get_available_frequencies(cpu);
463562e5f1aSJacob Tanenbaum 	if (freqs) {
464562e5f1aSJacob Tanenbaum 		printf(_("  available frequency steps:  "));
465562e5f1aSJacob Tanenbaum 		while (freqs->next) {
46635fdf42dSHuang Rui 			print_speed(freqs->frequency, no_rounding);
467562e5f1aSJacob Tanenbaum 			printf(", ");
468562e5f1aSJacob Tanenbaum 			freqs = freqs->next;
469562e5f1aSJacob Tanenbaum 		}
47035fdf42dSHuang Rui 		print_speed(freqs->frequency, no_rounding);
471562e5f1aSJacob Tanenbaum 		printf("\n");
47241ddb7e1SThomas Renninger 		cpufreq_put_available_frequencies(freqs);
473562e5f1aSJacob Tanenbaum 	}
474562e5f1aSJacob Tanenbaum 
475562e5f1aSJacob Tanenbaum 	get_available_governors(cpu);
476562e5f1aSJacob Tanenbaum 	get_policy(cpu);
477562e5f1aSJacob Tanenbaum 	if (get_freq_hardware(cpu, 1) < 0)
478562e5f1aSJacob Tanenbaum 		get_freq_kernel(cpu, 1);
479562e5f1aSJacob Tanenbaum 	get_boost_mode(cpu);
4808382dce5SHuang Rui 	get_perf_cap(cpu);
481562e5f1aSJacob Tanenbaum }
482562e5f1aSJacob Tanenbaum 
4837fe2f639SDominik Brodowski static struct option info_opts[] = {
48457ab3b08SSriram Raghunathan 	{"debug",	 no_argument,		 NULL,	 'e'},
48557ab3b08SSriram Raghunathan 	{"boost",	 no_argument,		 NULL,	 'b'},
48657ab3b08SSriram Raghunathan 	{"freq",	 no_argument,		 NULL,	 'f'},
48757ab3b08SSriram Raghunathan 	{"hwfreq",	 no_argument,		 NULL,	 'w'},
48857ab3b08SSriram Raghunathan 	{"hwlimits",	 no_argument,		 NULL,	 'l'},
48957ab3b08SSriram Raghunathan 	{"driver",	 no_argument,		 NULL,	 'd'},
49057ab3b08SSriram Raghunathan 	{"policy",	 no_argument,		 NULL,	 'p'},
49157ab3b08SSriram Raghunathan 	{"governors",	 no_argument,		 NULL,	 'g'},
49257ab3b08SSriram Raghunathan 	{"related-cpus",  no_argument,	 NULL,	 'r'},
49357ab3b08SSriram Raghunathan 	{"affected-cpus", no_argument,	 NULL,	 'a'},
49457ab3b08SSriram Raghunathan 	{"stats",	 no_argument,		 NULL,	 's'},
49557ab3b08SSriram Raghunathan 	{"latency",	 no_argument,		 NULL,	 'y'},
49657ab3b08SSriram Raghunathan 	{"proc",	 no_argument,		 NULL,	 'o'},
49757ab3b08SSriram Raghunathan 	{"human",	 no_argument,		 NULL,	 'm'},
49857ab3b08SSriram Raghunathan 	{"no-rounding", no_argument,	 NULL,	 'n'},
4998382dce5SHuang Rui 	{"performance", no_argument,	 NULL,	 'c'},
5007fe2f639SDominik Brodowski 	{ },
5017fe2f639SDominik Brodowski };
5027fe2f639SDominik Brodowski 
cmd_freq_info(int argc,char ** argv)5037fe2f639SDominik Brodowski int cmd_freq_info(int argc, char **argv)
5047fe2f639SDominik Brodowski {
5057fe2f639SDominik Brodowski 	extern char *optarg;
5067fe2f639SDominik Brodowski 	extern int optind, opterr, optopt;
5077fe2f639SDominik Brodowski 	int ret = 0, cont = 1;
5087fe2f639SDominik Brodowski 	unsigned int cpu = 0;
5097fe2f639SDominik Brodowski 	unsigned int human = 0;
5107fe2f639SDominik Brodowski 	int output_param = 0;
5117fe2f639SDominik Brodowski 
5127fe2f639SDominik Brodowski 	do {
5138382dce5SHuang Rui 		ret = getopt_long(argc, argv, "oefwldpgrasmybnc", info_opts,
514e091abc7SPrarit Bhargava 				  NULL);
5157fe2f639SDominik Brodowski 		switch (ret) {
5167fe2f639SDominik Brodowski 		case '?':
5177fe2f639SDominik Brodowski 			output_param = '?';
5187fe2f639SDominik Brodowski 			cont = 0;
5197fe2f639SDominik Brodowski 			break;
5207fe2f639SDominik Brodowski 		case -1:
5217fe2f639SDominik Brodowski 			cont = 0;
5227fe2f639SDominik Brodowski 			break;
5237fe2f639SDominik Brodowski 		case 'b':
5247fe2f639SDominik Brodowski 		case 'o':
5257fe2f639SDominik Brodowski 		case 'a':
5267fe2f639SDominik Brodowski 		case 'r':
5277fe2f639SDominik Brodowski 		case 'g':
5287fe2f639SDominik Brodowski 		case 'p':
5297fe2f639SDominik Brodowski 		case 'd':
5307fe2f639SDominik Brodowski 		case 'l':
5317fe2f639SDominik Brodowski 		case 'w':
5327fe2f639SDominik Brodowski 		case 'f':
5337fe2f639SDominik Brodowski 		case 'e':
5347fe2f639SDominik Brodowski 		case 's':
5357fe2f639SDominik Brodowski 		case 'y':
5368382dce5SHuang Rui 		case 'c':
5377fe2f639SDominik Brodowski 			if (output_param) {
5387fe2f639SDominik Brodowski 				output_param = -1;
5397fe2f639SDominik Brodowski 				cont = 0;
5407fe2f639SDominik Brodowski 				break;
5417fe2f639SDominik Brodowski 			}
5427fe2f639SDominik Brodowski 			output_param = ret;
5437fe2f639SDominik Brodowski 			break;
5447fe2f639SDominik Brodowski 		case 'm':
5457fe2f639SDominik Brodowski 			if (human) {
5467fe2f639SDominik Brodowski 				output_param = -1;
5477fe2f639SDominik Brodowski 				cont = 0;
5487fe2f639SDominik Brodowski 				break;
5497fe2f639SDominik Brodowski 			}
5507fe2f639SDominik Brodowski 			human = 1;
5517fe2f639SDominik Brodowski 			break;
552e091abc7SPrarit Bhargava 		case 'n':
553e091abc7SPrarit Bhargava 			no_rounding = 1;
554e091abc7SPrarit Bhargava 			break;
5557fe2f639SDominik Brodowski 		default:
5567fe2f639SDominik Brodowski 			fprintf(stderr, "invalid or unknown argument\n");
5577fe2f639SDominik Brodowski 			return EXIT_FAILURE;
5587fe2f639SDominik Brodowski 		}
5597fe2f639SDominik Brodowski 	} while (cont);
5607fe2f639SDominik Brodowski 
5617fe2f639SDominik Brodowski 	switch (output_param) {
5627fe2f639SDominik Brodowski 	case 'o':
5637fe2f639SDominik Brodowski 		if (!bitmask_isallclear(cpus_chosen)) {
5647fe2f639SDominik Brodowski 			printf(_("The argument passed to this tool can't be "
5657fe2f639SDominik Brodowski 				 "combined with passing a --cpu argument\n"));
5667fe2f639SDominik Brodowski 			return -EINVAL;
5677fe2f639SDominik Brodowski 		}
5687fe2f639SDominik Brodowski 		break;
5697fe2f639SDominik Brodowski 	case 0:
5707fe2f639SDominik Brodowski 		output_param = 'e';
5717fe2f639SDominik Brodowski 	}
5727fe2f639SDominik Brodowski 
5737fe2f639SDominik Brodowski 	ret = 0;
5747fe2f639SDominik Brodowski 
575*5975e255SSaket Kumar Bhaskar 	/* Default is: show output of base_cpu only */
5767fe2f639SDominik Brodowski 	if (bitmask_isallclear(cpus_chosen))
577*5975e255SSaket Kumar Bhaskar 		bitmask_setbit(cpus_chosen, base_cpu);
5787fe2f639SDominik Brodowski 
5797fe2f639SDominik Brodowski 	switch (output_param) {
5807fe2f639SDominik Brodowski 	case -1:
5817fe2f639SDominik Brodowski 		printf(_("You can't specify more than one --cpu parameter and/or\n"
5827fe2f639SDominik Brodowski 		       "more than one output-specific argument\n"));
5837fe2f639SDominik Brodowski 		return -EINVAL;
5847fe2f639SDominik Brodowski 	case '?':
5857fe2f639SDominik Brodowski 		printf(_("invalid or unknown argument\n"));
5867fe2f639SDominik Brodowski 		return -EINVAL;
5877fe2f639SDominik Brodowski 	case 'o':
5887fe2f639SDominik Brodowski 		proc_cpufreq_output();
5897fe2f639SDominik Brodowski 		return EXIT_SUCCESS;
5907fe2f639SDominik Brodowski 	}
5917fe2f639SDominik Brodowski 
5927fe2f639SDominik Brodowski 	for (cpu = bitmask_first(cpus_chosen);
5937fe2f639SDominik Brodowski 	     cpu <= bitmask_last(cpus_chosen); cpu++) {
5947fe2f639SDominik Brodowski 
5957fe2f639SDominik Brodowski 		if (!bitmask_isbitset(cpus_chosen, cpu))
5967fe2f639SDominik Brodowski 			continue;
597ce512b84SThomas Renninger 
598ce512b84SThomas Renninger 		printf(_("analyzing CPU %d:\n"), cpu);
599ce512b84SThomas Renninger 
600ce512b84SThomas Renninger 		if (sysfs_is_cpu_online(cpu) != 1) {
601ce512b84SThomas Renninger 			printf(_(" *is offline\n"));
602ce512b84SThomas Renninger 			printf("\n");
6037fe2f639SDominik Brodowski 			continue;
6047fe2f639SDominik Brodowski 		}
6057fe2f639SDominik Brodowski 
6067fe2f639SDominik Brodowski 		switch (output_param) {
6077fe2f639SDominik Brodowski 		case 'b':
6087fe2f639SDominik Brodowski 			get_boost_mode(cpu);
6097fe2f639SDominik Brodowski 			break;
6107fe2f639SDominik Brodowski 		case 'e':
6117fe2f639SDominik Brodowski 			debug_output_one(cpu);
6127fe2f639SDominik Brodowski 			break;
6137fe2f639SDominik Brodowski 		case 'a':
6147fe2f639SDominik Brodowski 			ret = get_affected_cpus(cpu);
6157fe2f639SDominik Brodowski 			break;
6167fe2f639SDominik Brodowski 		case 'r':
6177fe2f639SDominik Brodowski 			ret = get_related_cpus(cpu);
6187fe2f639SDominik Brodowski 			break;
6197fe2f639SDominik Brodowski 		case 'g':
6207fe2f639SDominik Brodowski 			ret = get_available_governors(cpu);
6217fe2f639SDominik Brodowski 			break;
6227fe2f639SDominik Brodowski 		case 'p':
6237fe2f639SDominik Brodowski 			ret = get_policy(cpu);
6247fe2f639SDominik Brodowski 			break;
6257fe2f639SDominik Brodowski 		case 'd':
6267fe2f639SDominik Brodowski 			ret = get_driver(cpu);
6277fe2f639SDominik Brodowski 			break;
6287fe2f639SDominik Brodowski 		case 'l':
6294da39cebSStafford Horne 			ret = get_hardware_limits(cpu, human);
6307fe2f639SDominik Brodowski 			break;
6317fe2f639SDominik Brodowski 		case 'w':
6327fe2f639SDominik Brodowski 			ret = get_freq_hardware(cpu, human);
6337fe2f639SDominik Brodowski 			break;
6347fe2f639SDominik Brodowski 		case 'f':
6357fe2f639SDominik Brodowski 			ret = get_freq_kernel(cpu, human);
6367fe2f639SDominik Brodowski 			break;
6377fe2f639SDominik Brodowski 		case 's':
6387fe2f639SDominik Brodowski 			ret = get_freq_stats(cpu, human);
6397fe2f639SDominik Brodowski 			break;
6407fe2f639SDominik Brodowski 		case 'y':
6417fe2f639SDominik Brodowski 			ret = get_latency(cpu, human);
6427fe2f639SDominik Brodowski 			break;
6438382dce5SHuang Rui 		case 'c':
6448382dce5SHuang Rui 			ret = get_perf_cap(cpu);
6458382dce5SHuang Rui 			break;
6467fe2f639SDominik Brodowski 		}
6477fe2f639SDominik Brodowski 		if (ret)
648a1ce5ba2SDominik Brodowski 			return ret;
6497fe2f639SDominik Brodowski 	}
6507fe2f639SDominik Brodowski 	return ret;
6517fe2f639SDominik Brodowski }
652