Merge tag 'block-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / power / cpupower / utils / cpupower-set.c
blob180d5ba877e6048c7176e95aac07c1acd3b9cd9f
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc.
4 */
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 #include <sys/utsname.h>
15 #include "helpers/helpers.h"
16 #include "helpers/sysfs.h"
17 #include "helpers/bitmask.h"
19 static struct option set_opts[] = {
20 {"perf-bias", required_argument, NULL, 'b'},
21 { },
24 static void print_wrong_arg_exit(void)
26 printf(_("invalid or unknown argument\n"));
27 exit(EXIT_FAILURE);
30 int cmd_set(int argc, char **argv)
32 extern char *optarg;
33 extern int optind, opterr, optopt;
34 unsigned int cpu;
35 struct utsname uts;
37 union {
38 struct {
39 int perf_bias:1;
41 int params;
42 } params;
43 int perf_bias = 0;
44 int ret = 0;
46 ret = uname(&uts);
47 if (!ret && (!strcmp(uts.machine, "ppc64le") ||
48 !strcmp(uts.machine, "ppc64"))) {
49 fprintf(stderr, _("Subcommand not supported on POWER.\n"));
50 return ret;
53 setlocale(LC_ALL, "");
54 textdomain(PACKAGE);
56 params.params = 0;
57 /* parameter parsing */
58 while ((ret = getopt_long(argc, argv, "b:",
59 set_opts, NULL)) != -1) {
60 switch (ret) {
61 case 'b':
62 if (params.perf_bias)
63 print_wrong_arg_exit();
64 perf_bias = atoi(optarg);
65 if (perf_bias < 0 || perf_bias > 15) {
66 printf(_("--perf-bias param out "
67 "of range [0-%d]\n"), 15);
68 print_wrong_arg_exit();
70 params.perf_bias = 1;
71 break;
72 default:
73 print_wrong_arg_exit();
77 if (!params.params)
78 print_wrong_arg_exit();
80 /* Default is: set all CPUs */
81 if (bitmask_isallclear(cpus_chosen))
82 bitmask_setall(cpus_chosen);
84 /* loop over CPUs */
85 for (cpu = bitmask_first(cpus_chosen);
86 cpu <= bitmask_last(cpus_chosen); cpu++) {
88 if (!bitmask_isbitset(cpus_chosen, cpu))
89 continue;
91 if (sysfs_is_cpu_online(cpu) != 1){
92 fprintf(stderr, _("Cannot set values on CPU %d:"), cpu);
93 fprintf(stderr, _(" *is offline\n"));
94 continue;
97 if (params.perf_bias) {
98 ret = cpupower_intel_set_perf_bias(cpu, perf_bias);
99 if (ret) {
100 fprintf(stderr, _("Error setting perf-bias "
101 "value on CPU %d\n"), cpu);
102 break;
106 return ret;