cpufreq.c (1bdd3e05a0a3b4a97ea88bc46fef8fb265c8b94c) cpufreq.c (1e4f63aecb53e48468661e922fc2fa3b83e55722)
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/drivers/cpufreq/cpufreq.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 *

--- 60 unchanged lines hidden (view full) ---

69
70/* internal prototypes */
71static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
72static int cpufreq_init_governor(struct cpufreq_policy *policy);
73static void cpufreq_exit_governor(struct cpufreq_policy *policy);
74static int cpufreq_start_governor(struct cpufreq_policy *policy);
75static void cpufreq_stop_governor(struct cpufreq_policy *policy);
76static void cpufreq_governor_limits(struct cpufreq_policy *policy);
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * linux/drivers/cpufreq/cpufreq.c
4 *
5 * Copyright (C) 2001 Russell King
6 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
7 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
8 *

--- 60 unchanged lines hidden (view full) ---

69
70/* internal prototypes */
71static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
72static int cpufreq_init_governor(struct cpufreq_policy *policy);
73static void cpufreq_exit_governor(struct cpufreq_policy *policy);
74static int cpufreq_start_governor(struct cpufreq_policy *policy);
75static void cpufreq_stop_governor(struct cpufreq_policy *policy);
76static void cpufreq_governor_limits(struct cpufreq_policy *policy);
77static int cpufreq_set_policy(struct cpufreq_policy *policy,
78 struct cpufreq_governor *new_gov,
79 unsigned int new_pol);
77
78/**
79 * Two notifier lists: the "policy" list is involved in the
80 * validation process for a new CPU frequency policy; the
81 * "transition" list for kernel code that needs to handle
82 * changes to devices when the CPU clock speed changes.
83 * The mutex locks both lists.
84 */

--- 526 unchanged lines hidden (view full) ---

611
612 for_each_governor(t)
613 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
614 return t;
615
616 return NULL;
617}
618
80
81/**
82 * Two notifier lists: the "policy" list is involved in the
83 * validation process for a new CPU frequency policy; the
84 * "transition" list for kernel code that needs to handle
85 * changes to devices when the CPU clock speed changes.
86 * The mutex locks both lists.
87 */

--- 526 unchanged lines hidden (view full) ---

614
615 for_each_governor(t)
616 if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
617 return t;
618
619 return NULL;
620}
621
619static int cpufreq_parse_policy(char *str_governor,
620 struct cpufreq_policy *policy)
622static unsigned int cpufreq_parse_policy(char *str_governor)
621{
623{
622 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
623 policy->policy = CPUFREQ_POLICY_PERFORMANCE;
624 return 0;
625 }
626 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN)) {
627 policy->policy = CPUFREQ_POLICY_POWERSAVE;
628 return 0;
629 }
630 return -EINVAL;
624 if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
625 return CPUFREQ_POLICY_PERFORMANCE;
626
627 if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
628 return CPUFREQ_POLICY_POWERSAVE;
629
630 return CPUFREQ_POLICY_UNKNOWN;
631}
632
633/**
634 * cpufreq_parse_governor - parse a governor string only for has_target()
631}
632
633/**
634 * cpufreq_parse_governor - parse a governor string only for has_target()
635 * @str_governor: Governor name.
635 */
636 */
636static int cpufreq_parse_governor(char *str_governor,
637 struct cpufreq_policy *policy)
637static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
638{
639 struct cpufreq_governor *t;
640
641 mutex_lock(&cpufreq_governor_mutex);
642
643 t = find_governor(str_governor);
644 if (!t) {
645 int ret;
646
647 mutex_unlock(&cpufreq_governor_mutex);
648
649 ret = request_module("cpufreq_%s", str_governor);
650 if (ret)
638{
639 struct cpufreq_governor *t;
640
641 mutex_lock(&cpufreq_governor_mutex);
642
643 t = find_governor(str_governor);
644 if (!t) {
645 int ret;
646
647 mutex_unlock(&cpufreq_governor_mutex);
648
649 ret = request_module("cpufreq_%s", str_governor);
650 if (ret)
651 return -EINVAL;
651 return NULL;
652
653 mutex_lock(&cpufreq_governor_mutex);
654
655 t = find_governor(str_governor);
656 }
657 if (t && !try_module_get(t->owner))
658 t = NULL;
659
660 mutex_unlock(&cpufreq_governor_mutex);
661
652
653 mutex_lock(&cpufreq_governor_mutex);
654
655 t = find_governor(str_governor);
656 }
657 if (t && !try_module_get(t->owner))
658 t = NULL;
659
660 mutex_unlock(&cpufreq_governor_mutex);
661
662 if (t) {
663 policy->governor = t;
664 return 0;
665 }
666
667 return -EINVAL;
662 return t;
668}
669
670/**
671 * cpufreq_per_cpu_attr_read() / show_##file_name() -
672 * print out cpufreq information
673 *
674 * Write out information from cpufreq_driver->policy[cpu]; object must be
675 * "unsigned int".

--- 84 unchanged lines hidden (view full) ---

760}
761
762/**
763 * store_scaling_governor - store policy for the specified CPU
764 */
765static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
766 const char *buf, size_t count)
767{
663}
664
665/**
666 * cpufreq_per_cpu_attr_read() / show_##file_name() -
667 * print out cpufreq information
668 *
669 * Write out information from cpufreq_driver->policy[cpu]; object must be
670 * "unsigned int".

--- 84 unchanged lines hidden (view full) ---

755}
756
757/**
758 * store_scaling_governor - store policy for the specified CPU
759 */
760static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
761 const char *buf, size_t count)
762{
763 char str_governor[16];
768 int ret;
764 int ret;
769 char str_governor[16];
770 struct cpufreq_policy new_policy;
771
765
772 memcpy(&new_policy, policy, sizeof(*policy));
773
774 ret = sscanf(buf, "%15s", str_governor);
775 if (ret != 1)
776 return -EINVAL;
777
778 if (cpufreq_driver->setpolicy) {
766 ret = sscanf(buf, "%15s", str_governor);
767 if (ret != 1)
768 return -EINVAL;
769
770 if (cpufreq_driver->setpolicy) {
779 if (cpufreq_parse_policy(str_governor, &new_policy))
771 unsigned int new_pol;
772
773 new_pol = cpufreq_parse_policy(str_governor);
774 if (!new_pol)
780 return -EINVAL;
775 return -EINVAL;
776
777 ret = cpufreq_set_policy(policy, NULL, new_pol);
781 } else {
778 } else {
782 if (cpufreq_parse_governor(str_governor, &new_policy))
779 struct cpufreq_governor *new_gov;
780
781 new_gov = cpufreq_parse_governor(str_governor);
782 if (!new_gov)
783 return -EINVAL;
783 return -EINVAL;
784 }
785
784
786 ret = cpufreq_set_policy(policy, &new_policy);
785 ret = cpufreq_set_policy(policy, new_gov,
786 CPUFREQ_POLICY_UNKNOWN);
787
787
788 if (new_policy.governor)
789 module_put(new_policy.governor->owner);
788 module_put(new_gov->owner);
789 }
790
791 return ret ? ret : count;
792}
793
794/**
795 * show_scaling_driver - show the cpufreq driver currently loaded
796 */
797static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)

--- 250 unchanged lines hidden (view full) ---

1048
1049__weak struct cpufreq_governor *cpufreq_default_governor(void)
1050{
1051 return NULL;
1052}
1053
1054static int cpufreq_init_policy(struct cpufreq_policy *policy)
1055{
790
791 return ret ? ret : count;
792}
793
794/**
795 * show_scaling_driver - show the cpufreq driver currently loaded
796 */
797static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)

--- 250 unchanged lines hidden (view full) ---

1048
1049__weak struct cpufreq_governor *cpufreq_default_governor(void)
1050{
1051 return NULL;
1052}
1053
1054static int cpufreq_init_policy(struct cpufreq_policy *policy)
1055{
1056 struct cpufreq_governor *gov = NULL, *def_gov = NULL;
1057 struct cpufreq_policy new_policy;
1056 struct cpufreq_governor *def_gov = cpufreq_default_governor();
1057 struct cpufreq_governor *gov = NULL;
1058 unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
1058
1059
1059 memcpy(&new_policy, policy, sizeof(*policy));
1060
1061 def_gov = cpufreq_default_governor();
1062
1063 if (has_target()) {
1060 if (has_target()) {
1064 /*
1065 * Update governor of new_policy to the governor used before
1066 * hotplug
1067 */
1061 /* Update policy governor to the one used before hotplug. */
1068 gov = find_governor(policy->last_governor);
1069 if (gov) {
1070 pr_debug("Restoring governor %s for cpu %d\n",
1062 gov = find_governor(policy->last_governor);
1063 if (gov) {
1064 pr_debug("Restoring governor %s for cpu %d\n",
1071 policy->governor->name, policy->cpu);
1072 } else {
1073 if (!def_gov)
1074 return -ENODATA;
1065 policy->governor->name, policy->cpu);
1066 } else if (def_gov) {
1075 gov = def_gov;
1067 gov = def_gov;
1068 } else {
1069 return -ENODATA;
1076 }
1070 }
1077 new_policy.governor = gov;
1078 } else {
1079 /* Use the default policy if there is no last_policy. */
1080 if (policy->last_policy) {
1071 } else {
1072 /* Use the default policy if there is no last_policy. */
1073 if (policy->last_policy) {
1081 new_policy.policy = policy->last_policy;
1074 pol = policy->last_policy;
1075 } else if (def_gov) {
1076 pol = cpufreq_parse_policy(def_gov->name);
1082 } else {
1077 } else {
1083 if (!def_gov)
1084 return -ENODATA;
1085 cpufreq_parse_policy(def_gov->name, &new_policy);
1078 return -ENODATA;
1086 }
1087 }
1088
1079 }
1080 }
1081
1089 return cpufreq_set_policy(policy, &new_policy);
1082 return cpufreq_set_policy(policy, gov, pol);
1090}
1091
1092static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1093{
1094 int ret = 0;
1095
1096 /* Has this CPU been taken care of already? */
1097 if (cpumask_test_cpu(cpu, policy->cpus))

--- 11 unchanged lines hidden (view full) ---

1109 pr_err("%s: Failed to start governor\n", __func__);
1110 }
1111 up_write(&policy->rwsem);
1112 return ret;
1113}
1114
1115void refresh_frequency_limits(struct cpufreq_policy *policy)
1116{
1083}
1084
1085static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1086{
1087 int ret = 0;
1088
1089 /* Has this CPU been taken care of already? */
1090 if (cpumask_test_cpu(cpu, policy->cpus))

--- 11 unchanged lines hidden (view full) ---

1102 pr_err("%s: Failed to start governor\n", __func__);
1103 }
1104 up_write(&policy->rwsem);
1105 return ret;
1106}
1107
1108void refresh_frequency_limits(struct cpufreq_policy *policy)
1109{
1117 struct cpufreq_policy new_policy;
1118
1119 if (!policy_is_inactive(policy)) {
1110 if (!policy_is_inactive(policy)) {
1120 new_policy = *policy;
1121 pr_debug("updating policy for CPU %u\n", policy->cpu);
1122
1111 pr_debug("updating policy for CPU %u\n", policy->cpu);
1112
1123 cpufreq_set_policy(policy, &new_policy);
1113 cpufreq_set_policy(policy, policy->governor, policy->policy);
1124 }
1125}
1126EXPORT_SYMBOL(refresh_frequency_limits);
1127
1128static void handle_update(struct work_struct *work)
1129{
1130 struct cpufreq_policy *policy =
1131 container_of(work, struct cpufreq_policy, update);

--- 1227 unchanged lines hidden (view full) ---

2359 cpufreq_cpu_put(cpu_policy);
2360 return 0;
2361}
2362EXPORT_SYMBOL(cpufreq_get_policy);
2363
2364/**
2365 * cpufreq_set_policy - Modify cpufreq policy parameters.
2366 * @policy: Policy object to modify.
1114 }
1115}
1116EXPORT_SYMBOL(refresh_frequency_limits);
1117
1118static void handle_update(struct work_struct *work)
1119{
1120 struct cpufreq_policy *policy =
1121 container_of(work, struct cpufreq_policy, update);

--- 1227 unchanged lines hidden (view full) ---

2349 cpufreq_cpu_put(cpu_policy);
2350 return 0;
2351}
2352EXPORT_SYMBOL(cpufreq_get_policy);
2353
2354/**
2355 * cpufreq_set_policy - Modify cpufreq policy parameters.
2356 * @policy: Policy object to modify.
2367 * @new_policy: New policy data.
2357 * @new_gov: Policy governor pointer.
2358 * @new_pol: Policy value (for drivers with built-in governors).
2368 *
2359 *
2369 * Pass @new_policy to the cpufreq driver's ->verify() callback. Next, copy the
2370 * min and max parameters of @new_policy to @policy and either invoke the
2371 * driver's ->setpolicy() callback (if present) or carry out a governor update
2372 * for @policy. That is, run the current governor's ->limits() callback (if the
2373 * governor field in @new_policy points to the same object as the one in
2374 * @policy) or replace the governor for @policy with the new one stored in
2375 * @new_policy.
2360 * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
2361 * limits to be set for the policy, update @policy with the verified limits
2362 * values and either invoke the driver's ->setpolicy() callback (if present) or
2363 * carry out a governor update for @policy. That is, run the current governor's
2364 * ->limits() callback (if @new_gov points to the same object as the one in
2365 * @policy) or replace the governor for @policy with @new_gov.
2376 *
2377 * The cpuinfo part of @policy is not updated by this function.
2378 */
2366 *
2367 * The cpuinfo part of @policy is not updated by this function.
2368 */
2379int cpufreq_set_policy(struct cpufreq_policy *policy,
2380 struct cpufreq_policy *new_policy)
2369static int cpufreq_set_policy(struct cpufreq_policy *policy,
2370 struct cpufreq_governor *new_gov,
2371 unsigned int new_pol)
2381{
2372{
2373 struct cpufreq_policy_data new_data;
2382 struct cpufreq_governor *old_gov;
2383 int ret;
2384
2374 struct cpufreq_governor *old_gov;
2375 int ret;
2376
2385 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2386 new_policy->cpu, new_policy->min, new_policy->max);
2387
2388 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2389
2377 memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
2378 new_data.freq_table = policy->freq_table;
2379 new_data.cpu = policy->cpu;
2390 /*
2391 * PM QoS framework collects all the requests from users and provide us
2392 * the final aggregated value here.
2393 */
2380 /*
2381 * PM QoS framework collects all the requests from users and provide us
2382 * the final aggregated value here.
2383 */
2394 new_policy->min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
2395 new_policy->max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
2384 new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
2385 new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
2396
2386
2387 pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
2388 new_data.cpu, new_data.min, new_data.max);
2389
2397 /*
2398 * Verify that the CPU speed can be set within these limits and make sure
2399 * that min <= max.
2400 */
2390 /*
2391 * Verify that the CPU speed can be set within these limits and make sure
2392 * that min <= max.
2393 */
2401 ret = cpufreq_driver->verify(new_policy);
2394 ret = cpufreq_driver->verify(&new_data);
2402 if (ret)
2403 return ret;
2404
2395 if (ret)
2396 return ret;
2397
2405 policy->min = new_policy->min;
2406 policy->max = new_policy->max;
2398 policy->min = new_data.min;
2399 policy->max = new_data.max;
2407 trace_cpu_frequency_limits(policy);
2408
2409 policy->cached_target_freq = UINT_MAX;
2410
2411 pr_debug("new min and max freqs are %u - %u kHz\n",
2412 policy->min, policy->max);
2413
2414 if (cpufreq_driver->setpolicy) {
2400 trace_cpu_frequency_limits(policy);
2401
2402 policy->cached_target_freq = UINT_MAX;
2403
2404 pr_debug("new min and max freqs are %u - %u kHz\n",
2405 policy->min, policy->max);
2406
2407 if (cpufreq_driver->setpolicy) {
2415 policy->policy = new_policy->policy;
2408 policy->policy = new_pol;
2416 pr_debug("setting range\n");
2417 return cpufreq_driver->setpolicy(policy);
2418 }
2419
2409 pr_debug("setting range\n");
2410 return cpufreq_driver->setpolicy(policy);
2411 }
2412
2420 if (new_policy->governor == policy->governor) {
2413 if (new_gov == policy->governor) {
2421 pr_debug("governor limits update\n");
2422 cpufreq_governor_limits(policy);
2423 return 0;
2424 }
2425
2426 pr_debug("governor switch\n");
2427
2428 /* save old, working values */
2429 old_gov = policy->governor;
2430 /* end old governor */
2431 if (old_gov) {
2432 cpufreq_stop_governor(policy);
2433 cpufreq_exit_governor(policy);
2434 }
2435
2436 /* start new governor */
2414 pr_debug("governor limits update\n");
2415 cpufreq_governor_limits(policy);
2416 return 0;
2417 }
2418
2419 pr_debug("governor switch\n");
2420
2421 /* save old, working values */
2422 old_gov = policy->governor;
2423 /* end old governor */
2424 if (old_gov) {
2425 cpufreq_stop_governor(policy);
2426 cpufreq_exit_governor(policy);
2427 }
2428
2429 /* start new governor */
2437 policy->governor = new_policy->governor;
2430 policy->governor = new_gov;
2438 ret = cpufreq_init_governor(policy);
2439 if (!ret) {
2440 ret = cpufreq_start_governor(policy);
2441 if (!ret) {
2442 pr_debug("governor change\n");
2443 sched_cpufreq_governor_change(policy, old_gov);
2444 return 0;
2445 }

--- 324 unchanged lines hidden ---
2431 ret = cpufreq_init_governor(policy);
2432 if (!ret) {
2433 ret = cpufreq_start_governor(policy);
2434 if (!ret) {
2435 pr_debug("governor change\n");
2436 sched_cpufreq_governor_change(policy, old_gov);
2437 return 0;
2438 }

--- 324 unchanged lines hidden ---