Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags   |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *  linux/drivers/cpufreq/cpufreq.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (C) 2001 Russell King
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *	Added handling for CPU hotplug
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  *	Fix handling for CPU hotplug -- affected CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/cpufreq_times.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/cpu_cooling.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/kernel_stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #include <linux/suspend.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #include <linux/syscore_ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #include <linux/tick.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #include <trace/events/power.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #include <trace/hooks/cpufreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) static LIST_HEAD(cpufreq_policy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) /* Macros to iterate over CPU policies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define for_each_suitable_policy(__policy, __active)			 \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 		if ((__active) == !policy_is_inactive(__policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) #define for_each_active_policy(__policy)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 	for_each_suitable_policy(__policy, true)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) #define for_each_inactive_policy(__policy)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) 	for_each_suitable_policy(__policy, false)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) #define for_each_policy(__policy)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /* Iterate over governors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) static LIST_HEAD(cpufreq_governor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define for_each_governor(__governor)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) static char default_governor[CPUFREQ_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58)  * The "cpufreq driver" - the arch- or hardware-dependent low
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59)  * level driver of CPUFreq support, and its spinlock. This lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60)  * also protects the cpufreq_cpu_data array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) static struct cpufreq_driver *cpufreq_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) static DEFINE_RWLOCK(cpufreq_driver_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) static DEFINE_STATIC_KEY_FALSE(cpufreq_freq_invariance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) bool cpufreq_supports_freq_invariance(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	return static_branch_likely(&cpufreq_freq_invariance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) /* Flag to suspend/resume CPUFreq governors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) static bool cpufreq_suspended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) static inline bool has_target(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	return cpufreq_driver->target_index || cpufreq_driver->target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* internal prototypes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) static int cpufreq_init_governor(struct cpufreq_policy *policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static void cpufreq_exit_governor(struct cpufreq_policy *policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) static void cpufreq_governor_limits(struct cpufreq_policy *policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) static int cpufreq_set_policy(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 			      struct cpufreq_governor *new_gov,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 			      unsigned int new_pol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90)  * Two notifier lists: the "policy" list is involved in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91)  * validation process for a new CPU frequency policy; the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92)  * "transition" list for kernel code that needs to handle
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93)  * changes to devices when the CPU clock speed changes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94)  * The mutex locks both lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) SRCU_NOTIFIER_HEAD_STATIC(cpufreq_transition_notifier_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) static int off __read_mostly;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) static int cpufreq_disabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 	return off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) void disable_cpufreq(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 	off = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) static DEFINE_MUTEX(cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) bool have_governor_per_policy(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) EXPORT_SYMBOL_GPL(have_governor_per_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) static struct kobject *cpufreq_global_kobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 	if (have_governor_per_policy())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 		return &policy->kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		return cpufreq_global_kobject;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	struct kernel_cpustat kcpustat;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 	u64 cur_wall_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	u64 idle_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 	u64 busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 	cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	kcpustat_cpu_fetch(&kcpustat, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 	busy_time = kcpustat.cpustat[CPUTIME_USER];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	busy_time += kcpustat.cpustat[CPUTIME_SYSTEM];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 	busy_time += kcpustat.cpustat[CPUTIME_IRQ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 	busy_time += kcpustat.cpustat[CPUTIME_SOFTIRQ];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	busy_time += kcpustat.cpustat[CPUTIME_STEAL];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	busy_time += kcpustat.cpustat[CPUTIME_NICE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 	idle_time = cur_wall_time - busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 	if (wall)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 		*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	return div_u64(idle_time, NSEC_PER_USEC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	if (idle_time == -1ULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 		return get_cpu_idle_time_jiffy(cpu, wall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 	else if (!io_busy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 		idle_time += get_cpu_iowait_time_us(cpu, wall);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	return idle_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) EXPORT_SYMBOL_GPL(get_cpu_idle_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166)  * This is a generic cpufreq init() routine which can be used by cpufreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167)  * drivers of SMP systems. It will do following:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168)  * - validate & show freq table passed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169)  * - set policies transition latency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170)  * - policy->cpus with all possible CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) void cpufreq_generic_init(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		struct cpufreq_frequency_table *table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 		unsigned int transition_latency)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	policy->freq_table = table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	policy->cpuinfo.transition_latency = transition_latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	 * The driver only supports the SMP configuration where all processors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 	 * share the clock and voltage and clock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	cpumask_setall(policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) EXPORT_SYMBOL_GPL(cpufreq_generic_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) unsigned int cpufreq_generic_get(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	if (!policy || IS_ERR(policy->clk)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		pr_err("%s: No %s associated to cpu: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		       __func__, policy ? "clk" : "policy", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	return clk_get_rate(policy->clk) / 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) EXPORT_SYMBOL_GPL(cpufreq_generic_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210)  * cpufreq_cpu_get - Return policy for a CPU and mark it as busy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211)  * @cpu: CPU to find the policy for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213)  * Call cpufreq_cpu_get_raw() to obtain a cpufreq policy for @cpu and increment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214)  * the kobject reference counter of that policy.  Return a valid policy on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215)  * success or NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217)  * The policy returned by this function has to be released with the help of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218)  * cpufreq_cpu_put() to balance its kobject reference counter properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	struct cpufreq_policy *policy = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	if (WARN_ON(cpu >= nr_cpu_ids))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 	/* get the cpufreq driver */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	read_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	if (cpufreq_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 		/* get the CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 		policy = cpufreq_cpu_get_raw(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 		if (policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 			kobject_get(&policy->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	return policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245)  * cpufreq_cpu_put - Decrement kobject usage counter for cpufreq policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246)  * @policy: cpufreq policy returned by cpufreq_cpu_get().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) void cpufreq_cpu_put(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	kobject_put(&policy->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255)  * cpufreq_cpu_release - Unlock a policy and decrement its usage counter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256)  * @policy: cpufreq policy returned by cpufreq_cpu_acquire().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) void cpufreq_cpu_release(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 	if (WARN_ON(!policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 	lockdep_assert_held(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * cpufreq_cpu_acquire - Find policy for a CPU, mark it as busy and lock it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * @cpu: CPU to find the policy for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  * Call cpufreq_cpu_get() to get a reference on the cpufreq policy for @cpu and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  * if the policy returned by it is not NULL, acquire its rwsem for writing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276)  * Return the policy if it is active or release it and return NULL otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278)  * The policy returned by this function has to be released with the help of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279)  * cpufreq_cpu_release() in order to release its rwsem and balance its usage
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280)  * counter properly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) struct cpufreq_policy *cpufreq_cpu_acquire(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (policy_is_inactive(policy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		cpufreq_cpu_release(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 	return policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300)  *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304)  * adjust_jiffies - adjust the system "loops_per_jiffy"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * This function alters the system "loops_per_jiffy" for the clock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  * speed change. Note that loops_per_jiffy cannot be updated on SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  * systems as each CPU might be scaled differently. So, use the arch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  * per-CPU loops_per_jiffy value wherever possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) #ifndef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	static unsigned long l_p_j_ref;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 	static unsigned int l_p_j_ref_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	if (ci->flags & CPUFREQ_CONST_LOOPS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	if (!l_p_j_ref_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		l_p_j_ref = loops_per_jiffy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		l_p_j_ref_freq = ci->old;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			 l_p_j_ref, l_p_j_ref_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 								ci->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 			 loops_per_jiffy, ci->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * cpufreq_notify_transition - Notify frequency transition and adjust_jiffies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * @policy: cpufreq policy to enable fast frequency switching for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * @freqs: contain details of the frequency update.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  * @state: set to CPUFREQ_PRECHANGE or CPUFREQ_POSTCHANGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341)  * This function calls the transition notifiers and the "adjust_jiffies"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342)  * function. It is called twice on all CPU frequency changes that have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343)  * external effects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) static void cpufreq_notify_transition(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 				      struct cpufreq_freqs *freqs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 				      unsigned int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 	BUG_ON(irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 	freqs->policy = policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 	freqs->flags = cpufreq_driver->flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	pr_debug("notification %u of frequency transition to %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		 state, freqs->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	case CPUFREQ_PRECHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		 * Detect if the driver reported a value as "old frequency"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		 * which is not equal to what the cpufreq core thinks is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		 * "old frequency".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		if (policy->cur && policy->cur != freqs->old) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 			pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 				 freqs->old, policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 			freqs->old = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 					 CPUFREQ_PRECHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	case CPUFREQ_POSTCHANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 		pr_debug("FREQ: %u - CPUs: %*pbl\n", freqs->new,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 			 cpumask_pr_args(policy->cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		for_each_cpu(cpu, policy->cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 			trace_cpu_frequency(freqs->new, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 					 CPUFREQ_POSTCHANGE, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		cpufreq_stats_record_transition(policy, freqs->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		cpufreq_times_record_transition(policy, freqs->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 		policy->cur = freqs->new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 		trace_android_rvh_cpufreq_transition(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) /* Do post notifications when there are chances that transition has failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 		struct cpufreq_freqs *freqs, int transition_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	if (!transition_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	swap(freqs->old, freqs->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		struct cpufreq_freqs *freqs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	 * Catch double invocations of _begin() which lead to self-deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	 * doesn't invoke _begin() on their behalf, and hence the chances of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	 * double invocations are very low. Moreover, there are scenarios
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 	 * where these checks can emit false-positive warnings in these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 	 * drivers; so we avoid that by skipping them altogether.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 				&& current == policy->transition_task);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) wait:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	wait_event(policy->transition_wait, !policy->transition_ongoing);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	spin_lock(&policy->transition_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (unlikely(policy->transition_ongoing)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		spin_unlock(&policy->transition_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		goto wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 	policy->transition_ongoing = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	policy->transition_task = current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	spin_unlock(&policy->transition_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		struct cpufreq_freqs *freqs, int transition_failed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	if (WARN_ON(!policy->transition_ongoing))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 	cpufreq_notify_post_transition(policy, freqs, transition_failed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	arch_set_freq_scale(policy->related_cpus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 			    policy->cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 			    policy->cpuinfo.max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 	policy->transition_ongoing = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	policy->transition_task = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	wake_up(&policy->transition_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465)  * Fast frequency switching status count.  Positive means "enabled", negative
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466)  * means "disabled" and 0 means "not decided yet".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) static int cpufreq_fast_switch_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) static DEFINE_MUTEX(cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) static void cpufreq_list_transition_notifiers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	struct notifier_block *nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	pr_info("Registered transition notifiers:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	mutex_lock(&cpufreq_transition_notifier_list.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		pr_info("%pS\n", nb->notifier_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486)  * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487)  * @policy: cpufreq policy to enable fast frequency switching for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489)  * Try to enable fast frequency switching for @policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491)  * The attempt will fail if there is at least one transition notifier registered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492)  * at this point, as fast frequency switching is quite fundamentally at odds
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493)  * with transition notifiers.  Thus if successful, it will make registration of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494)  * transition notifiers fail going forward.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 	lockdep_assert_held(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 	if (!policy->fast_switch_possible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	mutex_lock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	if (cpufreq_fast_switch_count >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		cpufreq_fast_switch_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		policy->fast_switch_enabled = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		pr_warn("CPU%u: Fast frequency switching not enabled\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		cpufreq_list_transition_notifiers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 	mutex_unlock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517)  * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518)  * @policy: cpufreq policy to disable fast frequency switching for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	mutex_lock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	if (policy->fast_switch_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		policy->fast_switch_enabled = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 			cpufreq_fast_switch_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	mutex_unlock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533)  * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534)  * one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535)  * @policy: associated policy to interrogate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536)  * @target_freq: target frequency to resolve.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538)  * The target to driver frequency mapping is cached in the policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540)  * Return: Lowest driver-supported frequency greater than or equal to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541)  * given target_freq, subject to policy (min/max) and driver limitations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 					 unsigned int target_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	unsigned int old_target_freq = target_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	target_freq = clamp_val(target_freq, policy->min, policy->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	trace_android_vh_cpufreq_resolve_freq(policy, target_freq, old_target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	policy->cached_target_freq = target_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (cpufreq_driver->target_index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		idx = cpufreq_frequency_table_target(policy, target_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 						     CPUFREQ_RELATION_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		policy->cached_resolved_idx = idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		return policy->freq_table[idx].frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	if (cpufreq_driver->resolve_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		return cpufreq_driver->resolve_freq(policy, target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 	return target_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	unsigned int latency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	if (policy->transition_delay_us)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 		return policy->transition_delay_us;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	if (latency) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		 * For platforms that can change the frequency very fast (< 10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		 * us), the above formula gives a decent transition delay. But
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 		 * for platforms where transition_latency is in milliseconds, it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 		 * ends up giving unrealistic values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 		 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 		 * Cap the default transition delay to 10 ms, which seems to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 		 * a reasonable amount of time after which we should reevaluate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		 * the frequency.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 		return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	return LATENCY_MULTIPLIER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595)  *                          SYSFS INTERFACE                          *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) static ssize_t show_boost(struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 			  struct kobj_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) static ssize_t store_boost(struct kobject *kobj, struct kobj_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 			   const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	int ret, enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	ret = sscanf(buf, "%d", &enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 	if (ret != 1 || enable < 0 || enable > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (cpufreq_boost_trigger_state(enable)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		pr_err("%s: Cannot %s BOOST!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 		       __func__, enable ? "enable" : "disable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	pr_debug("%s: cpufreq BOOST %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		 __func__, enable ? "enabled" : "disabled");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) define_one_global_rw(boost);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) static struct cpufreq_governor *find_governor(const char *str_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	struct cpufreq_governor *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	for_each_governor(t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 			return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) static struct cpufreq_governor *get_governor(const char *str_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 	struct cpufreq_governor *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	mutex_lock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	t = find_governor(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	if (!t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (!try_module_get(t->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		t = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	mutex_unlock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) static unsigned int cpufreq_parse_policy(char *str_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 		return CPUFREQ_POLICY_PERFORMANCE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	if (!strncasecmp(str_governor, "powersave", CPUFREQ_NAME_LEN))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		return CPUFREQ_POLICY_POWERSAVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 	return CPUFREQ_POLICY_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  * cpufreq_parse_governor - parse a governor string only for has_target()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667)  * @str_governor: Governor name.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) static struct cpufreq_governor *cpufreq_parse_governor(char *str_governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct cpufreq_governor *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	t = get_governor(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	if (t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		return t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	if (request_module("cpufreq_%s", str_governor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	return get_governor(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684)  * cpufreq_per_cpu_attr_read() / show_##file_name() -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685)  * print out cpufreq information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687)  * Write out information from cpufreq_driver->policy[cpu]; object must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688)  * "unsigned int".
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) #define show_one(file_name, object)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) static ssize_t show_##file_name				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) (struct cpufreq_policy *policy, char *buf)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) {							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	return sprintf(buf, "%u\n", policy->object);	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) static ssize_t show_cpuinfo_max_freq(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	unsigned int max_freq = policy->cpuinfo.max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	trace_android_vh_show_max_freq(policy, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	trace_android_rvh_show_max_freq(policy, &max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	return sprintf(buf, "%u\n", max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) show_one(cpuinfo_min_freq, cpuinfo.min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) show_one(scaling_min_freq, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) show_one(scaling_max_freq, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) __weak unsigned int arch_freq_get_on_cpu(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	unsigned int freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 	freq = arch_freq_get_on_cpu(policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	if (freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		ret = sprintf(buf, "%u\n", freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	else if (cpufreq_driver->setpolicy && cpufreq_driver->get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		ret = sprintf(buf, "%u\n", policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733)  * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) #define store_one(file_name, object)			\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) static ssize_t store_##file_name					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) (struct cpufreq_policy *policy, const char *buf, size_t count)		\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	unsigned long val;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 	int ret;							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 	ret = sscanf(buf, "%lu", &val);					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	if (ret != 1)							\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 		return -EINVAL;						\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	ret = freq_qos_update_request(policy->object##_freq_req, val);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	return ret >= 0 ? count : ret;					\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) store_one(scaling_min_freq, min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) store_one(scaling_max_freq, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754)  * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 					char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	unsigned int cur_freq = __cpufreq_get(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	if (cur_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 		return sprintf(buf, "%u\n", cur_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 	return sprintf(buf, "<unknown>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768)  * show_scaling_governor - show the current policy for the specified CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 		return sprintf(buf, "powersave\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		return sprintf(buf, "performance\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 	else if (policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 				policy->governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783)  * store_scaling_governor - store policy for the specified CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 					const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	char str_governor[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	ret = sscanf(buf, "%15s", str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	if (cpufreq_driver->setpolicy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 		unsigned int new_pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 		new_pol = cpufreq_parse_policy(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 		if (!new_pol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		ret = cpufreq_set_policy(policy, NULL, new_pol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		struct cpufreq_governor *new_gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		new_gov = cpufreq_parse_governor(str_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		if (!new_gov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 		ret = cpufreq_set_policy(policy, new_gov,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 					 CPUFREQ_POLICY_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 		module_put(new_gov->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	return ret ? ret : count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820)  * show_scaling_driver - show the cpufreq driver currently loaded
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828)  * show_scaling_available_governors - show the available CPUfreq governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 						char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	ssize_t i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	struct cpufreq_governor *t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 	if (!has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		i += sprintf(buf, "performance powersave");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	mutex_lock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	for_each_governor(t) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 		    - (CPUFREQ_NAME_LEN + 2)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 	mutex_unlock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	i += sprintf(&buf[i], "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 	ssize_t i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	unsigned int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	for_each_cpu(cpu, mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 		if (i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		if (i >= (PAGE_SIZE - 5))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 	i += sprintf(&buf[i], "\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872)  * show_related_cpus - show the CPUs affected by each transition even if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873)  * hw coordination is in use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	return cpufreq_show_cpus(policy->related_cpus, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881)  * show_affected_cpus - show the CPUs affected by each transition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	return cpufreq_show_cpus(policy->cpus, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 					const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	unsigned int freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	unsigned int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 	if (!policy->governor || !policy->governor->store_setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	ret = sscanf(buf, "%u", &freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	if (ret != 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	policy->governor->store_setspeed(policy, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	if (!policy->governor || !policy->governor->show_setspeed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		return sprintf(buf, "<unsupported>\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	return policy->governor->show_setspeed(policy, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915)  * show_bios_limit - show the current cpufreq HW/BIOS limitation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	unsigned int limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 	if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 		return sprintf(buf, "%u\n", limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) cpufreq_freq_attr_ro(cpuinfo_min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) cpufreq_freq_attr_ro(cpuinfo_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) cpufreq_freq_attr_ro(cpuinfo_transition_latency);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) cpufreq_freq_attr_ro(scaling_available_governors);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) cpufreq_freq_attr_ro(scaling_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) cpufreq_freq_attr_ro(scaling_cur_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) cpufreq_freq_attr_ro(bios_limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) cpufreq_freq_attr_ro(related_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) cpufreq_freq_attr_ro(affected_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) cpufreq_freq_attr_rw(scaling_min_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) cpufreq_freq_attr_rw(scaling_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) cpufreq_freq_attr_rw(scaling_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) cpufreq_freq_attr_rw(scaling_setspeed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static struct attribute *default_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	&cpuinfo_min_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	&cpuinfo_max_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 	&cpuinfo_transition_latency.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	&scaling_min_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	&scaling_max_freq.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	&affected_cpus.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	&related_cpus.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	&scaling_governor.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	&scaling_driver.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	&scaling_available_governors.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	&scaling_setspeed.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) #define to_attr(a) container_of(a, struct freq_attr, attr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	struct cpufreq_policy *policy = to_policy(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	struct freq_attr *fattr = to_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	ssize_t ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 	if (!fattr->show)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	down_read(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 	ret = fattr->show(policy, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 	up_read(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) static ssize_t store(struct kobject *kobj, struct attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 		     const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	struct cpufreq_policy *policy = to_policy(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	struct freq_attr *fattr = to_attr(attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	ssize_t ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	if (!fattr->store)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	 * cpus_read_trylock() is used here to work around a circular lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 	 * dependency problem with respect to the cpufreq_register_driver().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	if (!cpus_read_trylock())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	if (cpu_online(policy->cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 		down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 		ret = fattr->store(policy, buf, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 		up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) static void cpufreq_sysfs_release(struct kobject *kobj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	struct cpufreq_policy *policy = to_policy(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	pr_debug("last reference is dropped\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	complete(&policy->kobj_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) static const struct sysfs_ops sysfs_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	.show	= show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 	.store	= store,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) static struct kobj_type ktype_cpufreq = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	.sysfs_ops	= &sysfs_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	.default_attrs	= default_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	.release	= cpufreq_sysfs_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 				struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (unlikely(!dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	dev_dbg(dev, "%s: Adding symlink\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 		dev_err(dev, "cpufreq symlink creation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				   struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	dev_dbg(dev, "%s: Removing symlink\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	sysfs_remove_link(&dev->kobj, "cpufreq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	struct freq_attr **drv_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	/* set up files for this cpu device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	drv_attr = cpufreq_driver->attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	while (drv_attr && *drv_attr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		drv_attr++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 	if (cpufreq_driver->get) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	if (cpufreq_driver->bios_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static int cpufreq_init_policy(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	struct cpufreq_governor *gov = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	unsigned int pol = CPUFREQ_POLICY_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	if (has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 		/* Update policy governor to the one used before hotplug. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 		gov = get_governor(policy->last_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		if (gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 			pr_debug("Restoring governor %s for cpu %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 				 gov->name, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 			gov = get_governor(default_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		if (!gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 			gov = cpufreq_default_governor();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 			__module_get(gov->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 		/* Use the default policy if there is no last_policy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 		if (policy->last_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 			pol = policy->last_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 			pol = cpufreq_parse_policy(default_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 			 * In case the default governor is neither "performance"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 			 * nor "powersave", fall back to the initial policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 			 * value set by the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 			if (pol == CPUFREQ_POLICY_UNKNOWN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 				pol = policy->policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) 		if (pol != CPUFREQ_POLICY_PERFORMANCE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 		    pol != CPUFREQ_POLICY_POWERSAVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 			return -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) 	ret = cpufreq_set_policy(policy, gov, pol);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	if (gov)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 		module_put(gov->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	/* Has this CPU been taken care of already? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	if (cpumask_test_cpu(cpu, policy->cpus))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	if (has_target())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 		cpufreq_stop_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	cpumask_set_cpu(cpu, policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	if (has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		ret = cpufreq_start_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			pr_err("%s: Failed to start governor\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) void refresh_frequency_limits(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	if (!policy_is_inactive(policy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		pr_debug("updating policy for CPU %u\n", policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 		cpufreq_set_policy(policy, policy->governor, policy->policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) EXPORT_SYMBOL(refresh_frequency_limits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) static void handle_update(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	struct cpufreq_policy *policy =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 		container_of(work, struct cpufreq_policy, update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	pr_debug("handle_update for cpu %u called\n", policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	refresh_frequency_limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) static int cpufreq_notifier_min(struct notifier_block *nb, unsigned long freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 				void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	schedule_work(&policy->update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) static int cpufreq_notifier_max(struct notifier_block *nb, unsigned long freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 				void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 	struct cpufreq_policy *policy = container_of(nb, struct cpufreq_policy, nb_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 	schedule_work(&policy->update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	struct kobject *kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 	struct completion *cmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	cpufreq_stats_free_table(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 	kobj = &policy->kobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	cmp = &policy->kobj_unregister;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	kobject_put(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	 * We need to make sure that the underlying kobj is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	 * actually not referenced anymore by anybody before we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	 * proceed with unloading.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	pr_debug("waiting for dropping of refcount\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	wait_for_completion(cmp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	pr_debug("wait complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	struct device *dev = get_cpu_device(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 		goto err_free_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		goto err_free_cpumask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 		goto err_free_rcpumask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 				   cpufreq_global_kobject, "policy%u", cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 		dev_err(dev, "%s: failed to init policy->kobj: %d\n", __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		 * The entire policy object will be freed below, but the extra
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		 * memory allocated for the kobject name needs to be freed by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 		 * releasing the kobject.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		kobject_put(&policy->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		goto err_free_real_cpus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	freq_constraints_init(&policy->constraints);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	policy->nb_min.notifier_call = cpufreq_notifier_min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	policy->nb_max.notifier_call = cpufreq_notifier_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 				    &policy->nb_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		dev_err(dev, "Failed to register MIN QoS notifier: %d (%*pbl)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 			ret, cpumask_pr_args(policy->cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		goto err_kobj_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	ret = freq_qos_add_notifier(&policy->constraints, FREQ_QOS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 				    &policy->nb_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		dev_err(dev, "Failed to register MAX QoS notifier: %d (%*pbl)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 			ret, cpumask_pr_args(policy->cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		goto err_min_qos_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 	INIT_LIST_HEAD(&policy->policy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	init_rwsem(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	spin_lock_init(&policy->transition_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 	init_waitqueue_head(&policy->transition_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	init_completion(&policy->kobj_unregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	INIT_WORK(&policy->update, handle_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 	policy->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	return policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) err_min_qos_notifier:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 				 &policy->nb_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) err_kobj_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	cpufreq_policy_put_kobj(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) err_free_real_cpus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	free_cpumask_var(policy->real_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) err_free_rcpumask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 	free_cpumask_var(policy->related_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) err_free_cpumask:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	free_cpumask_var(policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) err_free_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 	kfree(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) static void cpufreq_policy_free(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 	/* Remove policy from list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 	list_del(&policy->policy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	for_each_cpu(cpu, policy->related_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		per_cpu(cpufreq_cpu_data, cpu) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 				 &policy->nb_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 	freq_qos_remove_notifier(&policy->constraints, FREQ_QOS_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 				 &policy->nb_min);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 	/* Cancel any pending policy->update work before freeing the policy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	cancel_work_sync(&policy->update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	if (policy->max_freq_req) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 		 * CPUFREQ_CREATE_POLICY notification is sent only after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		 * successfully adding max_freq_req request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 					     CPUFREQ_REMOVE_POLICY, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		freq_qos_remove_request(policy->max_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 	freq_qos_remove_request(policy->min_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	kfree(policy->min_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 	cpufreq_policy_put_kobj(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 	free_cpumask_var(policy->real_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 	free_cpumask_var(policy->related_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 	free_cpumask_var(policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 	kfree(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) static int cpufreq_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 	bool new_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	unsigned int j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 	/* Check if this CPU already has a policy to manage it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	policy = per_cpu(cpufreq_cpu_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		if (!policy_is_inactive(policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			return cpufreq_add_policy_cpu(policy, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 		/* This is the only online CPU for the policy.  Start over. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 		new_policy = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		policy->cpu = cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		policy->governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		new_policy = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		policy = cpufreq_policy_alloc(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 		if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	if (!new_policy && cpufreq_driver->online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 		ret = cpufreq_driver->online(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 			pr_debug("%s: %d: initialization failed\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 				 __LINE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 			goto out_exit_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 		/* Recover policy->cpus using related_cpus */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 		cpumask_copy(policy->cpus, policy->related_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 		cpumask_copy(policy->cpus, cpumask_of(cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		 * Call driver. From then on the cpufreq must be able
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		 * to accept all calls to ->verify and ->setpolicy for this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 		ret = cpufreq_driver->init(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 			pr_debug("%s: %d: initialization failed\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 				 __LINE__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 			goto out_free_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 		 * The initialization has succeeded and the policy is online.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 		 * If there is a problem with its frequency table, take it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 		 * offline and drop it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		ret = cpufreq_table_validate_and_sort(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 			goto out_offline_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 		/* related_cpus should at least include policy->cpus. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		cpumask_copy(policy->related_cpus, policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 	 * affected cpus must always be the one, which are online. We aren't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 	 * managing offline cpus here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	if (new_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		for_each_cpu(j, policy->related_cpus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 			per_cpu(cpufreq_cpu_data, j) = policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 			add_cpu_dev_symlink(policy, j, get_cpu_device(j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 		policy->min_freq_req = kzalloc(2 * sizeof(*policy->min_freq_req),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 					       GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		if (!policy->min_freq_req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 			goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 		ret = freq_qos_add_request(&policy->constraints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 					   policy->min_freq_req, FREQ_QOS_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 					   FREQ_QOS_MIN_DEFAULT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 			 * So we don't call freq_qos_remove_request() for an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 			 * uninitialized request.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 			kfree(policy->min_freq_req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 			policy->min_freq_req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 			goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 		 * This must be initialized right here to avoid calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 		 * freq_qos_remove_request() on uninitialized request in case
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 		 * of errors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 		policy->max_freq_req = policy->min_freq_req + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 		ret = freq_qos_add_request(&policy->constraints,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 					   policy->max_freq_req, FREQ_QOS_MAX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 					   FREQ_QOS_MAX_DEFAULT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 			policy->max_freq_req = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 			goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) 		blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 				CPUFREQ_CREATE_POLICY, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	if (cpufreq_driver->get && has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 		policy->cur = cpufreq_driver->get(policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		if (!policy->cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			pr_err("%s: ->get() failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	 * Sometimes boot loaders set CPU frequency to a value outside of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	 * frequency table present with cpufreq core. In such cases CPU might be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	 * unstable if it has to run on that frequency for long duration of time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	 * and so its better to set it to a frequency which is specified in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	 * freq-table. This also makes cpufreq stats inconsistent as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 	 * cpufreq-stats would fail to register because current frequency of CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 	 * isn't found in freq-table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	 * Because we don't want this change to effect boot process badly, we go
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	 * for the next freq which is >= policy->cur ('cur' must be set by now,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 	 * otherwise we will end up setting freq to lowest of the table as 'cur'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 	 * is initialized to zero).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 	 * We are passing target-freq as "policy->cur - 1" otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	 * equal to target-freq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 	    && has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		unsigned int old_freq = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		/* Are we running at unknown frequency ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 		ret = cpufreq_frequency_table_get_index(policy, old_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 		if (ret == -EINVAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 			ret = __cpufreq_driver_target(policy, old_freq - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 						      CPUFREQ_RELATION_L);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 			 * Reaching here after boot in a few seconds may not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 			 * mean that system will remain stable at "unknown"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 			 * frequency for longer duration. Hence, a BUG_ON().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 			BUG_ON(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 			pr_info("%s: CPU%d: Running at unlisted initial frequency: %u KHz, changing to: %u KHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 				__func__, policy->cpu, old_freq, policy->cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 	if (new_policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 		ret = cpufreq_add_dev_interface(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 			goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 		cpufreq_stats_create_table(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 		cpufreq_times_create_policy(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 		write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		list_add(&policy->policy_list, &cpufreq_policy_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	ret = cpufreq_init_policy(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 		pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 		       __func__, cpu, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 		goto out_destroy_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 	kobject_uevent(&policy->kobj, KOBJ_ADD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	/* Callback for handling stuff after policy is ready */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	if (cpufreq_driver->ready)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 		cpufreq_driver->ready(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	if (cpufreq_thermal_control_enabled(cpufreq_driver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 		policy->cdev = of_cpufreq_cooling_register(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 	pr_debug("initialization complete\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) out_destroy_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 	for_each_cpu(j, policy->real_cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 		remove_cpu_dev_symlink(policy, get_cpu_device(j));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) out_offline_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 	if (cpufreq_driver->offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 		cpufreq_driver->offline(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) out_exit_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 	if (cpufreq_driver->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 		cpufreq_driver->exit(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) out_free_policy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	cpufreq_policy_free(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550)  * cpufreq_add_dev - the cpufreq interface for a CPU device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551)  * @dev: CPU device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552)  * @sif: Subsystem interface structure pointer (not used)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 	unsigned cpu = dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 	if (cpu_online(cpu)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 		ret = cpufreq_online(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	/* Create sysfs link on CPU registration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	policy = per_cpu(cpufreq_cpu_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 	if (policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 		add_cpu_dev_symlink(policy, cpu, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) static int cpufreq_offline(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	policy = cpufreq_cpu_get_raw(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 	if (!policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 		pr_debug("%s: No cpu_data found\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	if (has_target())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 		cpufreq_stop_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	cpumask_clear_cpu(cpu, policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	if (policy_is_inactive(policy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 		if (has_target())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 			strncpy(policy->last_governor, policy->governor->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 				CPUFREQ_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 			policy->last_policy = policy->policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 	} else if (cpu == policy->cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		/* Nominate new CPU */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 		policy->cpu = cpumask_any(policy->cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	/* Start governor again for active policy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 	if (!policy_is_inactive(policy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 		if (has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 			ret = cpufreq_start_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 				pr_err("%s: Failed to start governor\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	if (cpufreq_thermal_control_enabled(cpufreq_driver)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 		cpufreq_cooling_unregister(policy->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		policy->cdev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 	if (cpufreq_driver->stop_cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		cpufreq_driver->stop_cpu(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	if (has_target())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		cpufreq_exit_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 	 * Perform the ->offline() during light-weight tear-down, as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	 * that allows fast recovery when the CPU comes back.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	if (cpufreq_driver->offline) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 		cpufreq_driver->offline(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	} else if (cpufreq_driver->exit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		cpufreq_driver->exit(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 		policy->freq_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645)  * cpufreq_remove_dev - remove a CPU device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647)  * Removes the cpufreq interface for a CPU device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 	unsigned int cpu = dev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	if (cpu_online(cpu))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		cpufreq_offline(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	cpumask_clear_cpu(cpu, policy->real_cpus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 	remove_cpu_dev_symlink(policy, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	if (cpumask_empty(policy->real_cpus)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 		/* We did light-weight exit earlier, do full tear down now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		if (cpufreq_driver->offline)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 			cpufreq_driver->exit(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 		cpufreq_policy_free(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)  *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674)  *	in deep trouble.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675)  *	@policy: policy managing CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676)  *	@new_freq: CPU frequency the CPU actually runs at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678)  *	We adjust to current frequency first, and need to clean up later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679)  *	So either call to cpufreq_update_policy() or schedule handle_update()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) static void cpufreq_out_of_sync(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 				unsigned int new_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	struct cpufreq_freqs freqs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 		 policy->cur, new_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	freqs.old = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 	freqs.new = new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	cpufreq_freq_transition_begin(policy, &freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	cpufreq_freq_transition_end(policy, &freqs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) static unsigned int cpufreq_verify_current_freq(struct cpufreq_policy *policy, bool update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	unsigned int new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	new_freq = cpufreq_driver->get(policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	if (!new_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) 	 * If fast frequency switching is used with the given policy, the check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) 	 * against policy->cur is pointless, so skip it in that case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 	if (policy->fast_switch_enabled || !has_target())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) 		return new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	if (policy->cur != new_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		cpufreq_out_of_sync(policy, new_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 		if (update)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 			schedule_work(&policy->update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	return new_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721)  * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)  * @cpu: CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724)  * This is the last known freq, without actually getting it from the driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725)  * Return value will be same as what is shown in scaling_cur_freq in sysfs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) unsigned int cpufreq_quick_get(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 	unsigned int ret_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 	read_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		ret_freq = cpufreq_driver->get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		read_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 		return ret_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) 	policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744) 	if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) 		ret_freq = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) 		cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749) 	return ret_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) EXPORT_SYMBOL(cpufreq_quick_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)  * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755)  * @cpu: CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)  * Just return the max possible frequency for a given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) unsigned int cpufreq_quick_get_max(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	unsigned int ret_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) 	if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 		ret_freq = policy->max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 		cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	return ret_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) EXPORT_SYMBOL(cpufreq_quick_get_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774)  * cpufreq_get_hw_max_freq - get the max hardware frequency of the CPU
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775)  * @cpu: CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777)  * The default return value is the max_freq field of cpuinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) __weak unsigned int cpufreq_get_hw_max_freq(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 	unsigned int ret_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 	if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		ret_freq = policy->cpuinfo.max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 	return ret_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) EXPORT_SYMBOL(cpufreq_get_hw_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) static unsigned int __cpufreq_get(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 	if (unlikely(policy_is_inactive(policy)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 	return cpufreq_verify_current_freq(policy, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802)  * cpufreq_get - get the current CPU frequency (in kHz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)  * @cpu: CPU number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805)  * Get the CPU current (static) CPU frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) unsigned int cpufreq_get(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 	unsigned int ret_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 	if (policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 		down_read(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 		if (cpufreq_driver->get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 			ret_freq = __cpufreq_get(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 		up_read(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 		cpufreq_cpu_put(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 	return ret_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) EXPORT_SYMBOL(cpufreq_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) static struct subsys_interface cpufreq_interface = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 	.name		= "cpufreq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 	.subsys		= &cpu_subsys,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 	.add_dev	= cpufreq_add_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 	.remove_dev	= cpufreq_remove_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833)  * In case platform wants some specific frequency to be configured
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)  * during suspend..
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) int cpufreq_generic_suspend(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 	if (!policy->suspend_freq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 		pr_debug("%s: suspend_freq not defined\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 	pr_debug("%s: Setting suspend-freq: %u\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 			policy->suspend_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 	ret = __cpufreq_driver_target(policy, policy->suspend_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 			CPUFREQ_RELATION_H);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 		pr_err("%s: unable to set suspend-freq: %u. err: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 				__func__, policy->suspend_freq, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) EXPORT_SYMBOL(cpufreq_generic_suspend);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859)  * cpufreq_suspend() - Suspend CPUFreq governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)  * Called during system wide Suspend/Hibernate cycles for suspending governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862)  * as some platforms can't change frequency after this point in suspend cycle.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863)  * Because some of the devices (like: i2c, regulators, etc) they use for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864)  * changing frequency are suspended quickly after this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) void cpufreq_suspend(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 	if (!cpufreq_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	if (!has_target() && !cpufreq_driver->suspend)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 		goto suspend;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 	pr_debug("%s: Suspending Governors\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 	for_each_active_policy(policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		if (has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 			down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 			cpufreq_stop_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 			up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) 		if (cpufreq_driver->suspend && cpufreq_driver->suspend(policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886) 			pr_err("%s: Failed to suspend driver: %s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) 				cpufreq_driver->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) suspend:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) 	cpufreq_suspended = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895)  * cpufreq_resume() - Resume CPUFreq governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)  * Called during system wide Suspend/Hibernate cycle for resuming governors that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898)  * are suspended with cpufreq_suspend().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) void cpufreq_resume(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 	if (!cpufreq_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	if (unlikely(!cpufreq_suspended))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	cpufreq_suspended = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 	if (!has_target() && !cpufreq_driver->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 	pr_debug("%s: Resuming Governors\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) 	for_each_active_policy(policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 		if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) 			pr_err("%s: Failed to resume driver: %p\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) 				policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) 		} else if (has_target()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) 			down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) 			ret = cpufreq_start_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) 			up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 			if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 				pr_err("%s: Failed to start governor for policy: %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 				       __func__, policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935)  * cpufreq_driver_test_flags - Test cpufreq driver's flags against given ones.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936)  * @flags: Flags to test against the current cpufreq driver's flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938)  * Assumes that the driver is there, so callers must ensure that this is the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939)  * case.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) bool cpufreq_driver_test_flags(u16 flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 	return !!(cpufreq_driver->flags & flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947)  *	cpufreq_get_current_driver - return current driver's name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949)  *	Return the name string of the currently loaded cpufreq driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)  *	or NULL, if none.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) const char *cpufreq_get_current_driver(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	if (cpufreq_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		return cpufreq_driver->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962)  *	cpufreq_get_driver_data - return current driver data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964)  *	Return the private data of the currently loaded cpufreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965)  *	driver, or NULL if no cpufreq driver is loaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) void *cpufreq_get_driver_data(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) 	if (cpufreq_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) 		return cpufreq_driver->driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) EXPORT_SYMBOL_GPL(cpufreq_get_driver_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977)  *                     NOTIFIER LISTS INTERFACE                      *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981)  *	cpufreq_register_notifier - register a driver with cpufreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982)  *	@nb: notifier function to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983)  *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985)  *	Add a driver to one of two lists: either a list of drivers that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986)  *      are notified about clock rate changes (once before and once after
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987)  *      the transition), or a list of drivers that are notified about
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988)  *      changes in cpufreq policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990)  *	This function may sleep, and has the same return conditions as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991)  *	blocking_notifier_chain_register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	switch (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	case CPUFREQ_TRANSITION_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		mutex_lock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 		if (cpufreq_fast_switch_count > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 			mutex_unlock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		ret = srcu_notifier_chain_register(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 				&cpufreq_transition_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 		if (!ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 			cpufreq_fast_switch_count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 		mutex_unlock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) 	case CPUFREQ_POLICY_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 		ret = blocking_notifier_chain_register(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 				&cpufreq_policy_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) EXPORT_SYMBOL(cpufreq_register_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028)  *	cpufreq_unregister_notifier - unregister a driver with cpufreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029)  *	@nb: notifier block to be unregistered
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030)  *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032)  *	Remove a driver from the CPU frequency notifier list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)  *	This function may sleep, and has the same return conditions as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035)  *	blocking_notifier_chain_unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 	switch (list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 	case CPUFREQ_TRANSITION_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 		mutex_lock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 		ret = srcu_notifier_chain_unregister(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 				&cpufreq_transition_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 		if (!ret && !WARN_ON(cpufreq_fast_switch_count >= 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 			cpufreq_fast_switch_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 		mutex_unlock(&cpufreq_fast_switch_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 	case CPUFREQ_POLICY_NOTIFIER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 		ret = blocking_notifier_chain_unregister(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 				&cpufreq_policy_notifier_list, nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) EXPORT_SYMBOL(cpufreq_unregister_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069)  *                              GOVERNORS                            *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073)  * cpufreq_driver_fast_switch - Carry out a fast CPU frequency switch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)  * @policy: cpufreq policy to switch the frequency for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075)  * @target_freq: New frequency to set (may be approximate).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077)  * Carry out a fast frequency switch without sleeping.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)  * The driver's ->fast_switch() callback invoked by this function must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080)  * suitable for being called from within RCU-sched read-side critical sections
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081)  * and it is expected to select the minimum available frequency greater than or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082)  * equal to @target_freq (CPUFREQ_RELATION_L).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084)  * This function must not be called if policy->fast_switch_enabled is unset.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086)  * Governors calling this function must guarantee that it will never be invoked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087)  * twice in parallel for the same policy and that it will never be called in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)  * parallel with either ->target() or ->target_index() for the same policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090)  * Returns the actual frequency set for the CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092)  * If 0 is returned by the driver's ->fast_switch() callback to indicate an
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093)  * error condition, the hardware configuration must be preserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) unsigned int cpufreq_driver_fast_switch(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 					unsigned int target_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 	unsigned int freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 	unsigned int old_target_freq = target_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 	int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 	target_freq = clamp_val(target_freq, policy->min, policy->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 	trace_android_vh_cpufreq_fast_switch(policy, target_freq, old_target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 	freq = cpufreq_driver->fast_switch(policy, target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 	if (!freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 	policy->cur = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 	arch_set_freq_scale(policy->related_cpus, freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			    policy->cpuinfo.max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 	cpufreq_stats_record_transition(policy, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 	cpufreq_times_record_transition(policy, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 	trace_android_rvh_cpufreq_transition(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 	if (trace_cpu_frequency_enabled()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		for_each_cpu(cpu, policy->cpus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 			trace_cpu_frequency(freq, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	return freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) EXPORT_SYMBOL_GPL(cpufreq_driver_fast_switch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) /* Must set freqs->new to intermediate frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) static int __target_intermediate(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 				 struct cpufreq_freqs *freqs, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 	freqs->new = cpufreq_driver->get_intermediate(policy, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 	/* We don't need to switch to intermediate freq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 	if (!freqs->new)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 	pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) 		 __func__, policy->cpu, freqs->old, freqs->new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	cpufreq_freq_transition_begin(policy, freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	ret = cpufreq_driver->target_intermediate(policy, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 	cpufreq_freq_transition_end(policy, freqs, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		pr_err("%s: Failed to change to intermediate frequency: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 		       __func__, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) static int __target_index(struct cpufreq_policy *policy, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153) 	struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) 	unsigned int intermediate_freq = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) 	unsigned int newfreq = policy->freq_table[index].frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) 	int retval = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) 	bool notify;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) 	if (newfreq == policy->cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 	if (notify) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 		/* Handle switching to intermediate frequency */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 		if (cpufreq_driver->get_intermediate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 			retval = __target_intermediate(policy, &freqs, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 			if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 				return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 			intermediate_freq = freqs.new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 			/* Set old freq to intermediate */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 			if (intermediate_freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 				freqs.old = freqs.new;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 		freqs.new = newfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 		pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			 __func__, policy->cpu, freqs.old, freqs.new);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 		cpufreq_freq_transition_begin(policy, &freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 	retval = cpufreq_driver->target_index(policy, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 	if (retval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 		pr_err("%s: Failed to change cpu frequency: %d\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 		       retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 	if (notify) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 		cpufreq_freq_transition_end(policy, &freqs, retval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 		 * Failed after setting to intermediate freq? Driver should have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 		 * reverted back to initial frequency and so should we. Check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		 * here for intermediate_freq instead of get_intermediate, in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 		 * case we haven't switched to intermediate freq at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		if (unlikely(retval && intermediate_freq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 			freqs.old = intermediate_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 			freqs.new = policy->restore_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 			cpufreq_freq_transition_begin(policy, &freqs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 			cpufreq_freq_transition_end(policy, &freqs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	return retval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) int __cpufreq_driver_target(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) 			    unsigned int target_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 			    unsigned int relation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	unsigned int old_target_freq = target_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 	/* Make sure that target_freq is within supported range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 	target_freq = clamp_val(target_freq, policy->min, policy->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	trace_android_vh_cpufreq_target(policy, target_freq, old_target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) 		 policy->cpu, target_freq, relation, old_target_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) 	 * This might look like a redundant call as we are checking it again
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) 	 * after finding index. But it is left intentionally for cases where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) 	 * exactly same freq is called again and so we can save on few function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229) 	 * calls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) 	if (target_freq == policy->cur &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	    !(cpufreq_driver->flags & CPUFREQ_NEED_UPDATE_LIMITS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	/* Save last value to restore later on errors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	policy->restore_freq = policy->cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	if (cpufreq_driver->target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 		return cpufreq_driver->target(policy, target_freq, relation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	if (!cpufreq_driver->target_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 	index = cpufreq_frequency_table_target(policy, target_freq, relation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 	return __target_index(policy, index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) int cpufreq_driver_target(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			  unsigned int target_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 			  unsigned int relation)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 	down_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 	ret = __cpufreq_driver_target(policy, target_freq, relation);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 	up_write(&policy->rwsem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) EXPORT_SYMBOL_GPL(cpufreq_driver_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) __weak struct cpufreq_governor *cpufreq_fallback_governor(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) static int cpufreq_init_governor(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 	/* Don't start any governor operations if we are entering suspend */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 	if (cpufreq_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	 * Governor might not be initiated here if ACPI _PPC changed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) 	 * notification happened, so check it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) 	if (!policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) 	/* Platform doesn't want dynamic frequency switching ? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) 	if (policy->governor->flags & CPUFREQ_GOV_DYNAMIC_SWITCHING &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) 	    cpufreq_driver->flags & CPUFREQ_NO_AUTO_DYNAMIC_SWITCHING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) 		struct cpufreq_governor *gov = cpufreq_fallback_governor();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) 		if (gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) 			pr_warn("Can't use %s governor as dynamic switching is disallowed. Fallback to %s governor\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 				policy->governor->name, gov->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 			policy->governor = gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 	if (!try_module_get(policy->governor->owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 	if (policy->governor->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 		ret = policy->governor->init(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 			module_put(policy->governor->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 	policy->strict_target = !!(policy->governor->flags & CPUFREQ_GOV_STRICT_TARGET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) static void cpufreq_exit_governor(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 	if (cpufreq_suspended || !policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	if (policy->governor->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 		policy->governor->exit(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 	module_put(policy->governor->owner);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) int cpufreq_start_governor(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) 	if (cpufreq_suspended)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) 	if (!policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	if (cpufreq_driver->get)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 		cpufreq_verify_current_freq(policy, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 	if (policy->governor->start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 		ret = policy->governor->start(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 	if (policy->governor->limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 		policy->governor->limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) void cpufreq_stop_governor(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	if (cpufreq_suspended || !policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	if (policy->governor->stop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		policy->governor->stop(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) static void cpufreq_governor_limits(struct cpufreq_policy *policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	if (cpufreq_suspended || !policy->governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	pr_debug("%s: for CPU %u\n", __func__, policy->cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 	if (policy->governor->limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 		policy->governor->limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) int cpufreq_register_governor(struct cpufreq_governor *governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	if (!governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 	mutex_lock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 	err = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	if (!find_governor(governor->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 		list_add(&governor->governor_list, &cpufreq_governor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	mutex_unlock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) EXPORT_SYMBOL_GPL(cpufreq_register_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) void cpufreq_unregister_governor(struct cpufreq_governor *governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	if (!governor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	/* clear last_governor for all inactive policies */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	read_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	for_each_inactive_policy(policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 		if (!strcmp(policy->last_governor, governor->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 			policy->governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 			strcpy(policy->last_governor, "\0");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 	mutex_lock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 	list_del(&governor->governor_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	mutex_unlock(&cpufreq_governor_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431)  *                          POLICY INTERFACE                         *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435)  * cpufreq_get_policy - get the current cpufreq_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)  * @policy: struct cpufreq_policy into which the current cpufreq_policy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437)  *	is written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)  * @cpu: CPU to find the policy for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440)  * Reads the current cpufreq policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 	struct cpufreq_policy *cpu_policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	cpu_policy = cpufreq_cpu_get(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 	if (!cpu_policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	memcpy(policy, cpu_policy, sizeof(*policy));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 	cpufreq_cpu_put(cpu_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457) EXPORT_SYMBOL(cpufreq_get_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  * cpufreq_set_policy - Modify cpufreq policy parameters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461)  * @policy: Policy object to modify.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462)  * @new_gov: Policy governor pointer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463)  * @new_pol: Policy value (for drivers with built-in governors).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465)  * Invoke the cpufreq driver's ->verify() callback to sanity-check the frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466)  * limits to be set for the policy, update @policy with the verified limits
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467)  * values and either invoke the driver's ->setpolicy() callback (if present) or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)  * carry out a governor update for @policy.  That is, run the current governor's
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469)  * ->limits() callback (if @new_gov points to the same object as the one in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470)  * @policy) or replace the governor for @policy with @new_gov.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472)  * The cpuinfo part of @policy is not updated by this function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) static int cpufreq_set_policy(struct cpufreq_policy *policy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 			      struct cpufreq_governor *new_gov,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 			      unsigned int new_pol)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 	struct cpufreq_policy_data new_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 	struct cpufreq_governor *old_gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 	memcpy(&new_data.cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 	new_data.freq_table = policy->freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 	new_data.cpu = policy->cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	 * PM QoS framework collects all the requests from users and provide us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 	 * the final aggregated value here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 	new_data.min = freq_qos_read_value(&policy->constraints, FREQ_QOS_MIN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 	new_data.max = freq_qos_read_value(&policy->constraints, FREQ_QOS_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 	pr_debug("setting new policy for CPU %u: %u - %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 		 new_data.cpu, new_data.min, new_data.max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 	 * Verify that the CPU speed can be set within these limits and make sure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 	 * that min <= max.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 	ret = cpufreq_driver->verify(&new_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 	policy->min = new_data.min;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 	policy->max = new_data.max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 	trace_cpu_frequency_limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 	policy->cached_target_freq = UINT_MAX;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	pr_debug("new min and max freqs are %u - %u kHz\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		 policy->min, policy->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	if (cpufreq_driver->setpolicy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) 		policy->policy = new_pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514) 		pr_debug("setting range\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) 		return cpufreq_driver->setpolicy(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518) 	if (new_gov == policy->governor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) 		pr_debug("governor limits update\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) 		cpufreq_governor_limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524) 	pr_debug("governor switch\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) 	/* save old, working values */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) 	old_gov = policy->governor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) 	/* end old governor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) 	if (old_gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) 		cpufreq_stop_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531) 		cpufreq_exit_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) 	/* start new governor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) 	policy->governor = new_gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) 	ret = cpufreq_init_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) 		ret = cpufreq_start_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) 		if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) 			pr_debug("governor change\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) 		cpufreq_exit_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) 	/* new governor failed, so re-start old one */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547) 	pr_debug("starting governor %s failed\n", policy->governor->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) 	if (old_gov) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) 		policy->governor = old_gov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) 		if (cpufreq_init_governor(policy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) 			policy->governor = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) 			cpufreq_start_governor(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) EXPORT_TRACEPOINT_SYMBOL_GPL(cpu_frequency_limits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561)  * cpufreq_update_policy - Re-evaluate an existing cpufreq policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562)  * @cpu: CPU to re-evaluate the policy for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564)  * Update the current frequency for the cpufreq policy of @cpu and use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565)  * cpufreq_set_policy() to re-apply the min and max limits, which triggers the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566)  * evaluation of policy notifiers and the cpufreq driver's ->verify() callback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567)  * for the policy in question, among other things.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) void cpufreq_update_policy(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) 	struct cpufreq_policy *policy = cpufreq_cpu_acquire(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573) 	if (!policy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) 	 * BIOS might change freq behind our back
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) 	 * -> ask driver for current freq and notify governors about a change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580) 	if (cpufreq_driver->get && has_target() &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) 	    (cpufreq_suspended || WARN_ON(!cpufreq_verify_current_freq(policy, false))))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) 		goto unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) 	refresh_frequency_limits(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587) 	cpufreq_cpu_release(policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) EXPORT_SYMBOL(cpufreq_update_policy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592)  * cpufreq_update_limits - Update policy limits for a given CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593)  * @cpu: CPU to update the policy limits for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595)  * Invoke the driver's ->update_limits callback if present or call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596)  * cpufreq_update_policy() for @cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598) void cpufreq_update_limits(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) 	if (cpufreq_driver->update_limits)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) 		cpufreq_driver->update_limits(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603) 		cpufreq_update_policy(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) EXPORT_SYMBOL_GPL(cpufreq_update_limits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608)  *               BOOST						     *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) static int cpufreq_boost_set_sw(struct cpufreq_policy *policy, int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) 	if (!policy->freq_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) 		return -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) 	ret = cpufreq_frequency_table_cpuinfo(policy, policy->freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) 		pr_err("%s: Policy frequency update failed\n", __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) 	ret = freq_qos_update_request(policy->max_freq_req, policy->max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) int cpufreq_boost_trigger_state(int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) 	struct cpufreq_policy *policy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) 	if (cpufreq_driver->boost_enabled == state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) 	cpufreq_driver->boost_enabled = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) 	get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644) 	for_each_active_policy(policy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) 		ret = cpufreq_driver->set_boost(policy, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) 			goto err_reset_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649) 	put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) err_reset_state:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) 	put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) 	cpufreq_driver->boost_enabled = !state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660) 	pr_err("%s: Cannot %s BOOST\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) 	       __func__, state ? "enable" : "disable");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) static bool cpufreq_boost_supported(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) 	return cpufreq_driver->set_boost;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) static int create_boost_sysfs_file(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) 	ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) 		pr_err("%s: cannot register global BOOST sysfs file\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) 		       __func__);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683) static void remove_boost_sysfs_file(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) 	if (cpufreq_boost_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) 		sysfs_remove_file(cpufreq_global_kobject, &boost.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) int cpufreq_enable_boost_support(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) 	if (!cpufreq_driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) 	if (cpufreq_boost_supported())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) 	cpufreq_driver->set_boost = cpufreq_boost_set_sw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) 	/* This will get removed on driver unregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) 	return create_boost_sysfs_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) int cpufreq_boost_enabled(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) 	return cpufreq_driver->boost_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) /*********************************************************************
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)  *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712)  *********************************************************************/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713) static enum cpuhp_state hp_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) static int cpuhp_cpufreq_online(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717) 	cpufreq_online(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) static int cpuhp_cpufreq_offline(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) 	cpufreq_offline(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730)  * cpufreq_register_driver - register a CPU Frequency driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731)  * @driver_data: A struct cpufreq_driver containing the values#
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732)  * submitted by the CPU Frequency driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734)  * Registers a CPU Frequency driver to this core code. This code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735)  * returns zero on success, -EEXIST when another driver got here first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736)  * (and isn't unregistered in the meantime).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) int cpufreq_register_driver(struct cpufreq_driver *driver_data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) 	 * The cpufreq core depends heavily on the availability of device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749) 	 * structure, make sure they are available before proceeding further.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) 	if (!get_cpu_device(0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) 		return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) 	if (!driver_data || !driver_data->verify || !driver_data->init ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) 	    !(driver_data->setpolicy || driver_data->target_index ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) 		    driver_data->target) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757) 	     (driver_data->setpolicy && (driver_data->target_index ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) 		    driver_data->target)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) 	     (!driver_data->get_intermediate != !driver_data->target_intermediate) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) 	     (!driver_data->online != !driver_data->offline))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) 	pr_debug("trying to register driver %s\n", driver_data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) 	/* Protect against concurrent CPU online/offline. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) 	if (cpufreq_driver) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) 		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771) 		ret = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) 	cpufreq_driver = driver_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) 	 * Mark support for the scheduler's frequency invariance engine for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) 	 * drivers that implement target(), target_index() or fast_switch().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781) 	if (!cpufreq_driver->setpolicy) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) 		static_branch_enable_cpuslocked(&cpufreq_freq_invariance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) 		pr_debug("supports frequency invariance");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) 	if (driver_data->setpolicy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) 		driver_data->flags |= CPUFREQ_CONST_LOOPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) 	if (cpufreq_boost_supported()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) 		ret = create_boost_sysfs_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) 			goto err_null_driver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) 	ret = subsys_interface_register(&cpufreq_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) 		goto err_boost_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) 	if (!(cpufreq_driver->flags & CPUFREQ_STICKY) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800) 	    list_empty(&cpufreq_policy_list)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) 		/* if all ->init() calls failed, unregister */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) 		ret = -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) 		pr_debug("%s: No CPU initialized for driver %s\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) 			 driver_data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) 		goto err_if_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808) 	ret = cpuhp_setup_state_nocalls_cpuslocked(CPUHP_AP_ONLINE_DYN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) 						   "cpufreq:online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) 						   cpuhp_cpufreq_online,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) 						   cpuhp_cpufreq_offline);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813) 		goto err_if_unreg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) 	hp_online = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) 	ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) 	pr_debug("driver %s up and running\n", driver_data->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) 	goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) err_if_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821) 	subsys_interface_unregister(&cpufreq_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) err_boost_unreg:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) 	remove_boost_sysfs_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824) err_null_driver:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) 	cpufreq_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) EXPORT_SYMBOL_GPL(cpufreq_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835)  * cpufreq_unregister_driver - unregister the current CPUFreq driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837)  * Unregister the current CPUFreq driver. Only call this if you have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838)  * the right to do so, i.e. if you have succeeded in initialising before!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839)  * Returns zero if successful, and -EINVAL if the cpufreq_driver is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840)  * currently not initialised.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842) int cpufreq_unregister_driver(struct cpufreq_driver *driver)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) 	if (!cpufreq_driver || (driver != cpufreq_driver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) 	pr_debug("unregistering driver %s\n", driver->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) 	/* Protect against concurrent cpu hotplug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852) 	cpus_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) 	subsys_interface_unregister(&cpufreq_interface);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) 	remove_boost_sysfs_file();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855) 	static_branch_disable_cpuslocked(&cpufreq_freq_invariance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) 	cpuhp_remove_state_nocalls_cpuslocked(hp_online);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) 	write_lock_irqsave(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) 	cpufreq_driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862) 	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) 	cpus_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867) EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) static int __init cpufreq_core_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) 	struct cpufreq_governor *gov = cpufreq_default_governor();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873) 	if (cpufreq_disabled())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) 	cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) 	BUG_ON(!cpufreq_global_kobject);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) 	if (!strlen(default_governor))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) 		strncpy(default_governor, gov->name, CPUFREQ_NAME_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) module_param(off, int, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) module_param_string(default_governor, default_governor, CPUFREQ_NAME_LEN, 0444);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886) core_initcall(cpufreq_core_init);