cpufreq.c (3a7e4fbbfd1a266ceea33e9b48f77f233ac994ac) cpufreq.c (a9909c217fb1430433ce791d52171a44ac5b937e)
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 *

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

47
48/* Iterate over governors */
49static LIST_HEAD(cpufreq_governor_list);
50#define for_each_governor(__governor) \
51 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
52
53static char default_governor[CPUFREQ_NAME_LEN];
54
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 *

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

47
48/* Iterate over governors */
49static LIST_HEAD(cpufreq_governor_list);
50#define for_each_governor(__governor) \
51 list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
52
53static char default_governor[CPUFREQ_NAME_LEN];
54
55/**
55/*
56 * The "cpufreq driver" - the arch- or hardware-dependent low
57 * level driver of CPUFreq support, and its spinlock. This lock
58 * also protects the cpufreq_cpu_data array.
59 */
60static struct cpufreq_driver *cpufreq_driver;
61static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
62static DEFINE_RWLOCK(cpufreq_driver_lock);
63

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

75static void cpufreq_exit_governor(struct cpufreq_policy *policy);
76static int cpufreq_start_governor(struct cpufreq_policy *policy);
77static void cpufreq_stop_governor(struct cpufreq_policy *policy);
78static void cpufreq_governor_limits(struct cpufreq_policy *policy);
79static int cpufreq_set_policy(struct cpufreq_policy *policy,
80 struct cpufreq_governor *new_gov,
81 unsigned int new_pol);
82
56 * The "cpufreq driver" - the arch- or hardware-dependent low
57 * level driver of CPUFreq support, and its spinlock. This lock
58 * also protects the cpufreq_cpu_data array.
59 */
60static struct cpufreq_driver *cpufreq_driver;
61static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
62static DEFINE_RWLOCK(cpufreq_driver_lock);
63

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

75static void cpufreq_exit_governor(struct cpufreq_policy *policy);
76static int cpufreq_start_governor(struct cpufreq_policy *policy);
77static void cpufreq_stop_governor(struct cpufreq_policy *policy);
78static void cpufreq_governor_limits(struct cpufreq_policy *policy);
79static int cpufreq_set_policy(struct cpufreq_policy *policy,
80 struct cpufreq_governor *new_gov,
81 unsigned int new_pol);
82
83/**
83/*
84 * Two notifier lists: the "policy" list is involved in the
85 * validation process for a new CPU frequency policy; the
86 * "transition" list for kernel code that needs to handle
87 * changes to devices when the CPU clock speed changes.
88 * The mutex locks both lists.
89 */
90static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
91SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);

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

295
296 return policy;
297}
298
299/*********************************************************************
300 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
301 *********************************************************************/
302
84 * Two notifier lists: the "policy" list is involved in the
85 * validation process for a new CPU frequency policy; the
86 * "transition" list for kernel code that needs to handle
87 * changes to devices when the CPU clock speed changes.
88 * The mutex locks both lists.
89 */
90static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
91SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);

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

295
296 return policy;
297}
298
299/*********************************************************************
300 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
301 *********************************************************************/
302
303/**
303/*
304 * adjust_jiffies - adjust the system "loops_per_jiffy"
305 *
306 * This function alters the system "loops_per_jiffy" for the clock
307 * speed change. Note that loops_per_jiffy cannot be updated on SMP
308 * systems as each CPU might be scaled differently. So, use the arch
309 * per-CPU loops_per_jiffy value wherever possible.
310 */
311static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)

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

521 }
522 mutex_unlock(&cpufreq_fast_switch_lock);
523}
524EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
525
526/**
527 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
528 * one.
304 * adjust_jiffies - adjust the system "loops_per_jiffy"
305 *
306 * This function alters the system "loops_per_jiffy" for the clock
307 * speed change. Note that loops_per_jiffy cannot be updated on SMP
308 * systems as each CPU might be scaled differently. So, use the arch
309 * per-CPU loops_per_jiffy value wherever possible.
310 */
311static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)

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

521 }
522 mutex_unlock(&cpufreq_fast_switch_lock);
523}
524EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
525
526/**
527 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
528 * one.
529 * @policy: associated policy to interrogate
529 * @target_freq: target frequency to resolve.
530 *
531 * The target to driver frequency mapping is cached in the policy.
532 *
533 * Return: Lowest driver-supported frequency greater than or equal to the
534 * given target_freq, subject to policy (min/max) and driver limitations.
535 */
536unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,

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

665 return t;
666
667 if (request_module("cpufreq_%s", str_governor))
668 return NULL;
669
670 return get_governor(str_governor);
671}
672
530 * @target_freq: target frequency to resolve.
531 *
532 * The target to driver frequency mapping is cached in the policy.
533 *
534 * Return: Lowest driver-supported frequency greater than or equal to the
535 * given target_freq, subject to policy (min/max) and driver limitations.
536 */
537unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,

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

666 return t;
667
668 if (request_module("cpufreq_%s", str_governor))
669 return NULL;
670
671 return get_governor(str_governor);
672}
673
673/**
674/*
674 * cpufreq_per_cpu_attr_read() / show_##file_name() -
675 * print out cpufreq information
676 *
677 * Write out information from cpufreq_driver->policy[cpu]; object must be
678 * "unsigned int".
679 */
680
681#define show_one(file_name, object) \

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

707 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
708 cpufreq_driver->get)
709 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
710 else
711 ret = sprintf(buf, "%u\n", policy->cur);
712 return ret;
713}
714
675 * cpufreq_per_cpu_attr_read() / show_##file_name() -
676 * print out cpufreq information
677 *
678 * Write out information from cpufreq_driver->policy[cpu]; object must be
679 * "unsigned int".
680 */
681
682#define show_one(file_name, object) \

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

708 else if (cpufreq_driver && cpufreq_driver->setpolicy &&
709 cpufreq_driver->get)
710 ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
711 else
712 ret = sprintf(buf, "%u\n", policy->cur);
713 return ret;
714}
715
715/**
716/*
716 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
717 */
718#define store_one(file_name, object) \
719static ssize_t store_##file_name \
720(struct cpufreq_policy *policy, const char *buf, size_t count) \
721{ \
722 unsigned long val; \
723 int ret; \

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

728 \
729 ret = freq_qos_update_request(policy->object##_freq_req, val);\
730 return ret >= 0 ? count : ret; \
731}
732
733store_one(scaling_min_freq, min);
734store_one(scaling_max_freq, max);
735
717 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
718 */
719#define store_one(file_name, object) \
720static ssize_t store_##file_name \
721(struct cpufreq_policy *policy, const char *buf, size_t count) \
722{ \
723 unsigned long val; \
724 int ret; \

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

729 \
730 ret = freq_qos_update_request(policy->object##_freq_req, val);\
731 return ret >= 0 ? count : ret; \
732}
733
734store_one(scaling_min_freq, min);
735store_one(scaling_max_freq, max);
736
736/**
737/*
737 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
738 */
739static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
740 char *buf)
741{
742 unsigned int cur_freq = __cpufreq_get(policy);
743
744 if (cur_freq)
745 return sprintf(buf, "%u\n", cur_freq);
746
747 return sprintf(buf, "<unknown>\n");
748}
749
738 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
739 */
740static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
741 char *buf)
742{
743 unsigned int cur_freq = __cpufreq_get(policy);
744
745 if (cur_freq)
746 return sprintf(buf, "%u\n", cur_freq);
747
748 return sprintf(buf, "<unknown>\n");
749}
750
750/**
751/*
751 * show_scaling_governor - show the current policy for the specified CPU
752 */
753static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
754{
755 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
756 return sprintf(buf, "powersave\n");
757 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
758 return sprintf(buf, "performance\n");
759 else if (policy->governor)
760 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
761 policy->governor->name);
762 return -EINVAL;
763}
764
752 * show_scaling_governor - show the current policy for the specified CPU
753 */
754static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
755{
756 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
757 return sprintf(buf, "powersave\n");
758 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
759 return sprintf(buf, "performance\n");
760 else if (policy->governor)
761 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
762 policy->governor->name);
763 return -EINVAL;
764}
765
765/**
766/*
766 * store_scaling_governor - store policy for the specified CPU
767 */
768static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
769 const char *buf, size_t count)
770{
771 char str_governor[16];
772 int ret;
773

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

794 CPUFREQ_POLICY_UNKNOWN);
795
796 module_put(new_gov->owner);
797 }
798
799 return ret ? ret : count;
800}
801
767 * store_scaling_governor - store policy for the specified CPU
768 */
769static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
770 const char *buf, size_t count)
771{
772 char str_governor[16];
773 int ret;
774

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

795 CPUFREQ_POLICY_UNKNOWN);
796
797 module_put(new_gov->owner);
798 }
799
800 return ret ? ret : count;
801}
802
802/**
803/*
803 * show_scaling_driver - show the cpufreq driver currently loaded
804 */
805static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
806{
807 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
808}
809
804 * show_scaling_driver - show the cpufreq driver currently loaded
805 */
806static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
807{
808 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
809}
810
810/**
811/*
811 * show_scaling_available_governors - show the available CPUfreq governors
812 */
813static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
814 char *buf)
815{
816 ssize_t i = 0;
817 struct cpufreq_governor *t;
818

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

846 if (i >= (PAGE_SIZE - 5))
847 break;
848 }
849 i += sprintf(&buf[i], "\n");
850 return i;
851}
852EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
853
812 * show_scaling_available_governors - show the available CPUfreq governors
813 */
814static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
815 char *buf)
816{
817 ssize_t i = 0;
818 struct cpufreq_governor *t;
819

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

847 if (i >= (PAGE_SIZE - 5))
848 break;
849 }
850 i += sprintf(&buf[i], "\n");
851 return i;
852}
853EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
854
854/**
855/*
855 * show_related_cpus - show the CPUs affected by each transition even if
856 * hw coordination is in use
857 */
858static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
859{
860 return cpufreq_show_cpus(policy->related_cpus, buf);
861}
862
856 * show_related_cpus - show the CPUs affected by each transition even if
857 * hw coordination is in use
858 */
859static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
860{
861 return cpufreq_show_cpus(policy->related_cpus, buf);
862}
863
863/**
864/*
864 * show_affected_cpus - show the CPUs affected by each transition
865 */
866static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
867{
868 return cpufreq_show_cpus(policy->cpus, buf);
869}
870
871static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,

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

889static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
890{
891 if (!policy->governor || !policy->governor->show_setspeed)
892 return sprintf(buf, "<unsupported>\n");
893
894 return policy->governor->show_setspeed(policy, buf);
895}
896
865 * show_affected_cpus - show the CPUs affected by each transition
866 */
867static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
868{
869 return cpufreq_show_cpus(policy->cpus, buf);
870}
871
872static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,

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

890static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
891{
892 if (!policy->governor || !policy->governor->show_setspeed)
893 return sprintf(buf, "<unsupported>\n");
894
895 return policy->governor->show_setspeed(policy, buf);
896}
897
897/**
898/*
898 * show_bios_limit - show the current cpufreq HW/BIOS limitation
899 */
900static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
901{
902 unsigned int limit;
903 int ret;
904 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
905 if (!ret)

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

1611 policy->freq_table = NULL;
1612 }
1613
1614unlock:
1615 up_write(&policy->rwsem);
1616 return 0;
1617}
1618
899 * show_bios_limit - show the current cpufreq HW/BIOS limitation
900 */
901static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
902{
903 unsigned int limit;
904 int ret;
905 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
906 if (!ret)

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

1612 policy->freq_table = NULL;
1613 }
1614
1615unlock:
1616 up_write(&policy->rwsem);
1617 return 0;
1618}
1619
1619/**
1620/*
1620 * cpufreq_remove_dev - remove a CPU device
1621 *
1622 * Removes the cpufreq interface for a CPU device.
1623 */
1624static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1625{
1626 unsigned int cpu = dev->id;
1627 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);

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

2368/*********************************************************************
2369 * POLICY INTERFACE *
2370 *********************************************************************/
2371
2372/**
2373 * cpufreq_get_policy - get the current cpufreq_policy
2374 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2375 * is written
1621 * cpufreq_remove_dev - remove a CPU device
1622 *
1623 * Removes the cpufreq interface for a CPU device.
1624 */
1625static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1626{
1627 unsigned int cpu = dev->id;
1628 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);

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

2369/*********************************************************************
2370 * POLICY INTERFACE *
2371 *********************************************************************/
2372
2373/**
2374 * cpufreq_get_policy - get the current cpufreq_policy
2375 * @policy: struct cpufreq_policy into which the current cpufreq_policy
2376 * is written
2377 * @cpu: CPU to find the policy for
2376 *
2377 * Reads the current cpufreq policy.
2378 */
2379int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2380{
2381 struct cpufreq_policy *cpu_policy;
2382 if (!policy)
2383 return -EINVAL;

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

2754 cpufreq_driver = NULL;
2755 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2756out:
2757 cpus_read_unlock();
2758 return ret;
2759}
2760EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2761
2378 *
2379 * Reads the current cpufreq policy.
2380 */
2381int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
2382{
2383 struct cpufreq_policy *cpu_policy;
2384 if (!policy)
2385 return -EINVAL;

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

2756 cpufreq_driver = NULL;
2757 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2758out:
2759 cpus_read_unlock();
2760 return ret;
2761}
2762EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2763
2762/**
2764/*
2763 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2764 *
2765 * Unregister the current CPUFreq driver. Only call this if you have
2766 * the right to do so, i.e. if you have succeeded in initialising before!
2767 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2768 * currently not initialised.
2769 */
2770int cpufreq_unregister_driver(struct cpufreq_driver *driver)

--- 43 unchanged lines hidden ---
2765 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2766 *
2767 * Unregister the current CPUFreq driver. Only call this if you have
2768 * the right to do so, i.e. if you have succeeded in initialising before!
2769 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2770 * currently not initialised.
2771 */
2772int cpufreq_unregister_driver(struct cpufreq_driver *driver)

--- 43 unchanged lines hidden ---