Thomas Gleixner | 4f19048 | 2019-05-27 08:55:14 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) 2011 Thomas Renninger <trenn@suse.de>, Novell Inc. |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 14 | #include "helpers/helpers.h" |
| 15 | #include "helpers/sysfs.h" |
| 16 | #include "helpers/bitmask.h" |
| 17 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 18 | static struct option set_opts[] = { |
Sriram Raghunathan | 57ab3b08 | 2015-10-23 09:52:45 +0200 | [diff] [blame] | 19 | {"perf-bias", required_argument, NULL, 'b'}, |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 20 | { }, |
| 21 | }; |
| 22 | |
| 23 | static void print_wrong_arg_exit(void) |
| 24 | { |
| 25 | printf(_("invalid or unknown argument\n")); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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 { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 37 | int perf_bias:1; |
| 38 | }; |
| 39 | int params; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 40 | } params; |
Thomas Renninger | 7ea1bdb | 2014-05-13 12:41:45 +0200 | [diff] [blame] | 41 | int perf_bias = 0; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 42 | int ret = 0; |
| 43 | |
| 44 | setlocale(LC_ALL, ""); |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 45 | textdomain(PACKAGE); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 46 | |
| 47 | params.params = 0; |
| 48 | /* parameter parsing */ |
Thomas Renninger | 7ea1bdb | 2014-05-13 12:41:45 +0200 | [diff] [blame] | 49 | while ((ret = getopt_long(argc, argv, "b:", |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 50 | set_opts, NULL)) != -1) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 51 | switch (ret) { |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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; |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 63 | default: |
| 64 | print_wrong_arg_exit(); |
| 65 | } |
| 66 | }; |
| 67 | |
Dominik Brodowski | 498ca79 | 2011-08-06 18:11:43 +0200 | [diff] [blame] | 68 | if (!params.params) |
| 69 | print_wrong_arg_exit(); |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 70 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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++) { |
Dominik Brodowski | a1ce5ba | 2011-04-19 20:33:50 +0200 | [diff] [blame] | 78 | |
Thomas Renninger | ce512b8 | 2015-12-01 17:14:15 +0100 | [diff] [blame] | 79 | if (!bitmask_isbitset(cpus_chosen, cpu)) |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 80 | continue; |
| 81 | |
Thomas Renninger | ce512b8 | 2015-12-01 17:14:15 +0100 | [diff] [blame] | 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 | |
Dominik Brodowski | 7fe2f63 | 2011-03-30 16:30:11 +0200 | [diff] [blame] | 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 | } |