14f19048fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27fe2f639SDominik Brodowski /*
37fe2f639SDominik Brodowski * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
47fe2f639SDominik Brodowski */
57fe2f639SDominik Brodowski
67fe2f639SDominik Brodowski
77fe2f639SDominik Brodowski #include <unistd.h>
87fe2f639SDominik Brodowski #include <stdio.h>
97fe2f639SDominik Brodowski #include <stdlib.h>
107fe2f639SDominik Brodowski #include <errno.h>
117fe2f639SDominik Brodowski #include <string.h>
127fe2f639SDominik Brodowski #include <getopt.h>
13d80a4ac2SAbhishek Goel #include <sys/utsname.h>
147fe2f639SDominik Brodowski
157fe2f639SDominik Brodowski #include "helpers/helpers.h"
167fe2f639SDominik Brodowski #include "helpers/sysfs.h"
177fe2f639SDominik Brodowski
187fe2f639SDominik Brodowski static struct option set_opts[] = {
1957ab3b08SSriram Raghunathan {"perf-bias", optional_argument, NULL, 'b'},
207fe2f639SDominik Brodowski { },
217fe2f639SDominik Brodowski };
227fe2f639SDominik Brodowski
print_wrong_arg_exit(void)237fe2f639SDominik Brodowski static void print_wrong_arg_exit(void)
247fe2f639SDominik Brodowski {
257fe2f639SDominik Brodowski printf(_("invalid or unknown argument\n"));
267fe2f639SDominik Brodowski exit(EXIT_FAILURE);
277fe2f639SDominik Brodowski }
287fe2f639SDominik Brodowski
cmd_info(int argc,char ** argv)297fe2f639SDominik Brodowski int cmd_info(int argc, char **argv)
307fe2f639SDominik Brodowski {
317fe2f639SDominik Brodowski extern char *optarg;
327fe2f639SDominik Brodowski extern int optind, opterr, optopt;
337fe2f639SDominik Brodowski unsigned int cpu;
34d80a4ac2SAbhishek Goel struct utsname uts;
357fe2f639SDominik Brodowski
367fe2f639SDominik Brodowski union {
377fe2f639SDominik Brodowski struct {
387fe2f639SDominik Brodowski int perf_bias:1;
397fe2f639SDominik Brodowski };
407fe2f639SDominik Brodowski int params;
417fe2f639SDominik Brodowski } params = {};
427fe2f639SDominik Brodowski int ret = 0;
437fe2f639SDominik Brodowski
44d80a4ac2SAbhishek Goel ret = uname(&uts);
45d80a4ac2SAbhishek Goel if (!ret && (!strcmp(uts.machine, "ppc64le") ||
46d80a4ac2SAbhishek Goel !strcmp(uts.machine, "ppc64"))) {
47d80a4ac2SAbhishek Goel fprintf(stderr, _("Subcommand not supported on POWER.\n"));
48d80a4ac2SAbhishek Goel return ret;
49d80a4ac2SAbhishek Goel }
50d80a4ac2SAbhishek Goel
517fe2f639SDominik Brodowski setlocale(LC_ALL, "");
527fe2f639SDominik Brodowski textdomain(PACKAGE);
537fe2f639SDominik Brodowski
547fe2f639SDominik Brodowski /* parameter parsing */
557ea1bdb8SThomas Renninger while ((ret = getopt_long(argc, argv, "b", set_opts, NULL)) != -1) {
567fe2f639SDominik Brodowski switch (ret) {
577fe2f639SDominik Brodowski case 'b':
587fe2f639SDominik Brodowski if (params.perf_bias)
597fe2f639SDominik Brodowski print_wrong_arg_exit();
607fe2f639SDominik Brodowski params.perf_bias = 1;
617fe2f639SDominik Brodowski break;
627fe2f639SDominik Brodowski default:
637fe2f639SDominik Brodowski print_wrong_arg_exit();
647fe2f639SDominik Brodowski }
657b0bf99bSZou Wei }
667fe2f639SDominik Brodowski
677fe2f639SDominik Brodowski if (!params.params)
687fe2f639SDominik Brodowski params.params = 0x7;
697fe2f639SDominik Brodowski
70*5975e255SSaket Kumar Bhaskar /* Default is: show output of base_cpu only */
717fe2f639SDominik Brodowski if (bitmask_isallclear(cpus_chosen))
72*5975e255SSaket Kumar Bhaskar bitmask_setbit(cpus_chosen, base_cpu);
737fe2f639SDominik Brodowski
747fe2f639SDominik Brodowski /* Add more per cpu options here */
757fe2f639SDominik Brodowski if (!params.perf_bias)
767fe2f639SDominik Brodowski return ret;
777fe2f639SDominik Brodowski
787fe2f639SDominik Brodowski if (params.perf_bias) {
797fe2f639SDominik Brodowski if (!run_as_root) {
807fe2f639SDominik Brodowski params.perf_bias = 0;
817fe2f639SDominik Brodowski printf(_("Intel's performance bias setting needs root privileges\n"));
827fe2f639SDominik Brodowski } else if (!(cpupower_cpu_info.caps & CPUPOWER_CAP_PERF_BIAS)) {
837fe2f639SDominik Brodowski printf(_("System does not support Intel's performance"
847fe2f639SDominik Brodowski " bias setting\n"));
857fe2f639SDominik Brodowski params.perf_bias = 0;
867fe2f639SDominik Brodowski }
877fe2f639SDominik Brodowski }
887fe2f639SDominik Brodowski
897fe2f639SDominik Brodowski /* loop over CPUs */
907fe2f639SDominik Brodowski for (cpu = bitmask_first(cpus_chosen);
917fe2f639SDominik Brodowski cpu <= bitmask_last(cpus_chosen); cpu++) {
927fe2f639SDominik Brodowski
93ce512b84SThomas Renninger if (!bitmask_isbitset(cpus_chosen, cpu))
947fe2f639SDominik Brodowski continue;
957fe2f639SDominik Brodowski
967fe2f639SDominik Brodowski printf(_("analyzing CPU %d:\n"), cpu);
977fe2f639SDominik Brodowski
98ce512b84SThomas Renninger if (sysfs_is_cpu_online(cpu) != 1){
99ce512b84SThomas Renninger printf(_(" *is offline\n"));
100ce512b84SThomas Renninger continue;
101ce512b84SThomas Renninger }
102ce512b84SThomas Renninger
1037fe2f639SDominik Brodowski if (params.perf_bias) {
1048113ab20SBorislav Petkov ret = cpupower_intel_get_perf_bias(cpu);
1057fe2f639SDominik Brodowski if (ret < 0) {
1063fc5a0e5SThomas Renninger fprintf(stderr,
1073fc5a0e5SThomas Renninger _("Could not read perf-bias value[%d]\n"), ret);
1083fc5a0e5SThomas Renninger exit(EXIT_FAILURE);
1097fe2f639SDominik Brodowski } else
1107fe2f639SDominik Brodowski printf(_("perf-bias: %d\n"), ret);
1117fe2f639SDominik Brodowski }
1127fe2f639SDominik Brodowski }
1133fc5a0e5SThomas Renninger return 0;
1147fe2f639SDominik Brodowski }
115