2 * (C) 2004-2009 Dominik Brodowski <linux@dominikbrodowski.de>
4 * Licensed under the terms of the GNU GPL License version 2.
19 #include "helpers/helpers.h"
21 #define NORM_FREQ_LEN 32
23 void freq_set_help(void)
25 printf(_("Usage: cpupower frequency-set [options]\n"));
26 printf(_("Options:\n"));
27 printf(_(" -d FREQ, --min FREQ new minimum CPU frequency the governor may select\n"));
28 printf(_(" -u FREQ, --max FREQ new maximum CPU frequency the governor may select\n"));
29 printf(_(" -g GOV, --governor GOV new cpufreq governor\n"));
30 printf(_(" -f FREQ, --freq FREQ specific frequency to be set. Requires userspace\n"
31 " governor to be available and loaded\n"));
32 printf(_(" -r, --related Switches all hardware-related CPUs\n"));
33 printf(_(" -h, --help Prints out this screen\n"));
36 "1. Omitting the -c or --cpu argument is equivalent to setting it to \"all\"\n"));
37 printf(_("2. The -f FREQ, --freq FREQ parameter cannot be combined with any other parameter\n"
38 " except the -c CPU, --cpu CPU parameter\n"
39 "3. FREQuencies can be passed in Hz, kHz (default), MHz, GHz, or THz\n"
40 " by postfixing the value with the wanted unit name, without any space\n"
41 " (FREQuency in kHz =^ Hz * 0.001 =^ MHz * 1000 =^ GHz * 1000000).\n"));
45 static struct option set_opts[] = {
46 { .name="min", .has_arg=required_argument, .flag=NULL, .val='d'},
47 { .name="max", .has_arg=required_argument, .flag=NULL, .val='u'},
48 { .name="governor", .has_arg=required_argument, .flag=NULL, .val='g'},
49 { .name="freq", .has_arg=required_argument, .flag=NULL, .val='f'},
50 { .name="help", .has_arg=no_argument, .flag=NULL, .val='h'},
51 { .name="related", .has_arg=no_argument, .flag=NULL, .val='r'},
55 static void print_error(void)
57 printf(_("Error setting new values. Common errors:\n"
58 "- Do you have proper administration rights? (super-user?)\n"
59 "- Is the governor you requested available and modprobed?\n"
60 "- Trying to set an invalid policy?\n"
61 "- Trying to set a specific frequency, but userspace governor is not available,\n"
62 " for example because of hardware which cannot be set to a specific frequency\n"
63 " or because the userspace governor isn't loaded?\n"));
71 const struct freq_units def_units[] = {
73 {"khz", 0}, /* default */
80 static void print_unknown_arg(void)
82 printf(_("invalid or unknown argument\n"));
86 static unsigned long string_to_frequency(const char *str)
88 char normalized[NORM_FREQ_LEN];
89 const struct freq_units *unit;
93 int power = 0, match_count = 0, i, cp, pad;
98 for (scan = str; isdigit(*scan) || *scan == '.'; scan++) {
99 if (*scan == '.' && match_count == 0)
101 else if (*scan == '.' && match_count == 1)
107 for (unit = def_units; unit->str_unit; unit++) {
109 scan[i] && tolower(scan[i]) == unit->str_unit[i];
115 power = unit->power_of_ten;
117 if (match_count != 1)
121 /* count the number of digits to be copied */
122 for (cp = 0; isdigit(str[cp]); cp++)
125 if (str[cp] == '.') {
126 while (power > -1 && isdigit(str[cp+1]))
129 if (power >= -1) /* not enough => pad */
131 else /* to much => strip */
132 pad = 0, cp += power + 1;
134 if (cp <= 0 || cp + pad > NORM_FREQ_LEN - 1)
138 for (i = 0; i < cp; i++, str++) {
141 normalized[i] = *str;
144 for (; i < cp + pad; i++)
147 /* round up, down ? */
148 match_count = (normalized[i-1] >= '5');
149 /* and drop the decimal part */
150 normalized[i-1] = 0; /* cp > 0 && pad >= 0 ==> i > 0 */
152 /* final conversion (and applying rounding) */
154 freq = strtoul(normalized, &end, 10);
158 if (match_count && freq != ULONG_MAX)
164 static int do_new_policy(unsigned int cpu, struct cpufreq_policy *new_pol)
166 struct cpufreq_policy *cur_pol = cpufreq_get_policy(cpu);
170 printf(_("wrong, unknown or unhandled CPU?\n"));
175 new_pol->min = cur_pol->min;
178 new_pol->max = cur_pol->max;
180 if (!new_pol->governor)
181 new_pol->governor = cur_pol->governor;
183 ret = cpufreq_set_policy(cpu, new_pol);
185 cpufreq_put_policy(cur_pol);
191 static int do_one_cpu(unsigned int cpu, struct cpufreq_policy *new_pol,
192 unsigned long freq, unsigned int pc)
196 return cpufreq_set_frequency(cpu, freq);
199 /* if only one value of a policy is to be changed, we can
203 return cpufreq_modify_policy_min(cpu, new_pol->min);
204 else if (new_pol->max)
205 return cpufreq_modify_policy_max(cpu, new_pol->max);
206 else if (new_pol->governor)
207 return cpufreq_modify_policy_governor(cpu, new_pol->governor);
211 return do_new_policy(cpu, new_pol);
215 int cmd_freq_set(int argc, char **argv)
218 extern int optind, opterr, optopt;
219 int ret = 0, cont = 1;
220 int double_parm = 0, related = 0, policychange = 0;
221 unsigned long freq = 0;
225 struct cpufreq_policy new_pol = {
231 /* parameter parsing */
233 ret = getopt_long(argc, argv, "d:u:g:f:hr", set_opts, NULL);
253 new_pol.min = string_to_frequency(optarg);
254 if (new_pol.min == 0) {
263 new_pol.max = string_to_frequency(optarg);
264 if (new_pol.max == 0) {
272 freq = string_to_frequency(optarg);
279 if (new_pol.governor)
282 if ((strlen(optarg) < 3) || (strlen(optarg) > 18)) {
286 if ((sscanf(optarg, "%s", gov)) != 1) {
290 new_pol.governor = gov;
295 /* parameter checking */
297 printf("the same parameter was passed more than once\n");
301 if (freq && policychange) {
302 printf(_("the -f/--freq parameter cannot be combined with -d/--min, -u/--max or\n"
303 "-g/--governor parameters\n"));
307 if (!freq && !policychange) {
308 printf(_("At least one parameter out of -f/--freq, -d/--min, -u/--max, and\n"
309 "-g/--governor must be passed\n"));
313 /* Default is: set all CPUs */
314 if (bitmask_isallclear(cpus_chosen))
315 bitmask_setall(cpus_chosen);
317 /* Also set frequency settings for related CPUs if -r is passed */
319 for (cpu = bitmask_first(cpus_chosen);
320 cpu <= bitmask_last(cpus_chosen); cpu++) {
321 struct cpufreq_affected_cpus *cpus;
323 if (!bitmask_isbitset(cpus_chosen, cpu) ||
324 cpufreq_cpu_exists(cpu))
327 cpus = cpufreq_get_related_cpus(cpu);
331 bitmask_setbit(cpus_chosen, cpus->cpu);
334 cpufreq_put_related_cpus(cpus);
340 for (cpu = bitmask_first(cpus_chosen);
341 cpu <= bitmask_last(cpus_chosen); cpu++) {
343 if (!bitmask_isbitset(cpus_chosen, cpu) ||
344 cpufreq_cpu_exists(cpu))
347 printf(_("Setting cpu: %d\n"), cpu);
348 ret = do_one_cpu(cpu, &new_pol, freq, policychange);