Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[sfrench/cifs-2.6.git] / tools / power / cpupower / utils / cpupower-set.c
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 #include "helpers/bitmask.h"
17
18 static struct option set_opts[] = {
19         {"perf-bias", required_argument, NULL, 'b'},
20         { },
21 };
22
23 static void print_wrong_arg_exit(void)
24 {
25         printf(_("invalid or unknown argument\n"));
26         exit(EXIT_FAILURE);
27 }
28
29 int cmd_set(int argc, char **argv)
30 {
31         extern char *optarg;
32         extern int optind, opterr, optopt;
33         unsigned int cpu;
34
35         union {
36                 struct {
37                         int perf_bias:1;
38                 };
39                 int params;
40         } params;
41         int perf_bias = 0;
42         int ret = 0;
43
44         setlocale(LC_ALL, "");
45         textdomain(PACKAGE);
46
47         params.params = 0;
48         /* parameter parsing */
49         while ((ret = getopt_long(argc, argv, "b:",
50                                                 set_opts, NULL)) != -1) {
51                 switch (ret) {
52                 case 'b':
53                         if (params.perf_bias)
54                                 print_wrong_arg_exit();
55                         perf_bias = atoi(optarg);
56                         if (perf_bias < 0 || perf_bias > 15) {
57                                 printf(_("--perf-bias param out "
58                                          "of range [0-%d]\n"), 15);
59                                 print_wrong_arg_exit();
60                         }
61                         params.perf_bias = 1;
62                         break;
63                 default:
64                         print_wrong_arg_exit();
65                 }
66         };
67
68         if (!params.params)
69                 print_wrong_arg_exit();
70
71         /* Default is: set all CPUs */
72         if (bitmask_isallclear(cpus_chosen))
73                 bitmask_setall(cpus_chosen);
74
75         /* loop over CPUs */
76         for (cpu = bitmask_first(cpus_chosen);
77              cpu <= bitmask_last(cpus_chosen); cpu++) {
78
79                 if (!bitmask_isbitset(cpus_chosen, cpu))
80                         continue;
81
82                 if (sysfs_is_cpu_online(cpu) != 1){
83                         fprintf(stderr, _("Cannot set values on CPU %d:"), cpu);
84                         fprintf(stderr, _(" *is offline\n"));
85                         continue;
86                 }
87
88                 if (params.perf_bias) {
89                         ret = msr_intel_set_perf_bias(cpu, perf_bias);
90                         if (ret) {
91                                 fprintf(stderr, _("Error setting perf-bias "
92                                                   "value on CPU %d\n"), cpu);
93                                 break;
94                         }
95                 }
96         }
97         return ret;
98 }