1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4  */
5 
6 
7 #include <unistd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <string.h>
12 #include <getopt.h>
13 
14 #include "helpers/helpers.h"
15 #include "helpers/sysfs.h"
16 
17 static struct option set_opts[] = {
18      {"perf-bias", optional_argument, NULL, 'b'},
19      { },
20 };
21 
22 static void print_wrong_arg_exit(void)
23 {
24 	printf(_("invalid or unknown argument\n"));
25 	exit(EXIT_FAILURE);
26 }
27 
28 int cmd_info(int argc, char **argv)
29 {
30 	extern char *optarg;
31 	extern int optind, opterr, optopt;
32 	unsigned int cpu;
33 
34 	union {
35 		struct {
36 			int perf_bias:1;
37 		};
38 		int params;
39 	} params = {};
40 	int ret = 0;
41 
42 	setlocale(LC_ALL, "");
43 	textdomain(PACKAGE);
44 
45 	/* parameter parsing */
46 	while ((ret = getopt_long(argc, argv, "b", set_opts, NULL)) != -1) {
47 		switch (ret) {
48 		case 'b':
49 			if (params.perf_bias)
50 				print_wrong_arg_exit();
51 			params.perf_bias = 1;
52 			break;
53 		default:
54 			print_wrong_arg_exit();
55 		}
56 	};
57 
58 	if (!params.params)
59 		params.params = 0x7;
60 
61 	/* Default is: show output of CPU 0 only */
62 	if (bitmask_isallclear(cpus_chosen))
63 		bitmask_setbit(cpus_chosen, 0);
64 
65 	/* Add more per cpu options here */
66 	if (!params.perf_bias)
67 		return ret;
68 
69 	if (params.perf_bias) {
70 		if (!run_as_root) {
71 			params.perf_bias = 0;
72 			printf(_("Intel's performance bias setting needs root privileges\n"));
73 		} else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
74 			printf(_("System does not support Intel's performance"
75 				 " bias setting\n"));
76 			params.perf_bias = 0;
77 		}
78 	}
79 
80 	/* loop over CPUs */
81 	for (cpu = bitmask_first(cpus_chosen);
82 	     cpu <= bitmask_last(cpus_chosen); cpu++) {
83 
84 		if (!bitmask_isbitset(cpus_chosen, cpu))
85 			continue;
86 
87 		printf(_("analyzing CPU %d:\n"), cpu);
88 
89 		if (sysfs_is_cpu_online(cpu) != 1){
90 			printf(_(" *is offline\n"));
91 			continue;
92 		}
93 
94 		if (params.perf_bias) {
95 			ret = msr_intel_get_perf_bias(cpu);
96 			if (ret < 0) {
97 				fprintf(stderr,
98 			_("Could not read perf-bias value[%d]\n"), ret);
99 				exit(EXIT_FAILURE);
100 			} else
101 				printf(_("perf-bias: %d\n"), ret);
102 		}
103 	}
104 	return 0;
105 }
106