11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/drivers/cpufreq/cpufreq.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 2001 Russell King 51da177e4SLinus Torvalds * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> 6bb176f7dSViresh Kumar * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org> 71da177e4SLinus Torvalds * 8c32b6b8eSAshok Raj * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 9c32b6b8eSAshok Raj * Added handling for CPU hotplug 108ff69732SDave Jones * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 118ff69732SDave Jones * Fix handling for CPU hotplug -- affected CPUs 12c32b6b8eSAshok Raj * 131da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 141da177e4SLinus Torvalds * it under the terms of the GNU General Public License version 2 as 151da177e4SLinus Torvalds * published by the Free Software Foundation. 161da177e4SLinus Torvalds */ 171da177e4SLinus Torvalds 18db701151SViresh Kumar #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 19db701151SViresh Kumar 205ff0a268SViresh Kumar #include <linux/cpu.h> 211da177e4SLinus Torvalds #include <linux/cpufreq.h> 221da177e4SLinus Torvalds #include <linux/delay.h> 231da177e4SLinus Torvalds #include <linux/device.h> 245ff0a268SViresh Kumar #include <linux/init.h> 255ff0a268SViresh Kumar #include <linux/kernel_stat.h> 265ff0a268SViresh Kumar #include <linux/module.h> 273fc54d37Sakpm@osdl.org #include <linux/mutex.h> 285ff0a268SViresh Kumar #include <linux/slab.h> 292f0aea93SViresh Kumar #include <linux/suspend.h> 3090de2a4aSDoug Anderson #include <linux/syscore_ops.h> 315ff0a268SViresh Kumar #include <linux/tick.h> 326f4f2723SThomas Renninger #include <trace/events/power.h> 336f4f2723SThomas Renninger 34b4f0676fSViresh Kumar static LIST_HEAD(cpufreq_policy_list); 35f963735aSViresh Kumar 36f963735aSViresh Kumar static inline bool policy_is_inactive(struct cpufreq_policy *policy) 37f963735aSViresh Kumar { 38f963735aSViresh Kumar return cpumask_empty(policy->cpus); 39f963735aSViresh Kumar } 40f963735aSViresh Kumar 41f963735aSViresh Kumar static bool suitable_policy(struct cpufreq_policy *policy, bool active) 42f963735aSViresh Kumar { 43f963735aSViresh Kumar return active == !policy_is_inactive(policy); 44f963735aSViresh Kumar } 45f963735aSViresh Kumar 46f963735aSViresh Kumar /* Finds Next Acive/Inactive policy */ 47f963735aSViresh Kumar static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy, 48f963735aSViresh Kumar bool active) 49f963735aSViresh Kumar { 50f963735aSViresh Kumar do { 51f963735aSViresh Kumar policy = list_next_entry(policy, policy_list); 52f963735aSViresh Kumar 53f963735aSViresh Kumar /* No more policies in the list */ 54f963735aSViresh Kumar if (&policy->policy_list == &cpufreq_policy_list) 55f963735aSViresh Kumar return NULL; 56f963735aSViresh Kumar } while (!suitable_policy(policy, active)); 57f963735aSViresh Kumar 58f963735aSViresh Kumar return policy; 59f963735aSViresh Kumar } 60f963735aSViresh Kumar 61f963735aSViresh Kumar static struct cpufreq_policy *first_policy(bool active) 62f963735aSViresh Kumar { 63f963735aSViresh Kumar struct cpufreq_policy *policy; 64f963735aSViresh Kumar 65f963735aSViresh Kumar /* No policies in the list */ 66f963735aSViresh Kumar if (list_empty(&cpufreq_policy_list)) 67f963735aSViresh Kumar return NULL; 68f963735aSViresh Kumar 69f963735aSViresh Kumar policy = list_first_entry(&cpufreq_policy_list, typeof(*policy), 70f963735aSViresh Kumar policy_list); 71f963735aSViresh Kumar 72f963735aSViresh Kumar if (!suitable_policy(policy, active)) 73f963735aSViresh Kumar policy = next_policy(policy, active); 74f963735aSViresh Kumar 75f963735aSViresh Kumar return policy; 76f963735aSViresh Kumar } 77f963735aSViresh Kumar 78f963735aSViresh Kumar /* Macros to iterate over CPU policies */ 79f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 80f963735aSViresh Kumar for (__policy = first_policy(__active); \ 81f963735aSViresh Kumar __policy; \ 82f963735aSViresh Kumar __policy = next_policy(__policy, __active)) 83f963735aSViresh Kumar 84f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 85f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 86f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 87f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 88f963735aSViresh Kumar 89b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 90b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 91b4f0676fSViresh Kumar 92f7b27061SViresh Kumar /* Iterate over governors */ 93f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 94f7b27061SViresh Kumar #define for_each_governor(__governor) \ 95f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 96f7b27061SViresh Kumar 971da177e4SLinus Torvalds /** 98cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 991da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 1001da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 1011da177e4SLinus Torvalds */ 1021c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 1037a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 104bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 1056f1e4efdSJane Li DEFINE_MUTEX(cpufreq_governor_lock); 106bb176f7dSViresh Kumar 1072f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 1082f0aea93SViresh Kumar static bool cpufreq_suspended; 1091da177e4SLinus Torvalds 1109c0ebcf7SViresh Kumar static inline bool has_target(void) 1119c0ebcf7SViresh Kumar { 1129c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 1139c0ebcf7SViresh Kumar } 1149c0ebcf7SViresh Kumar 1155a01f2e8SVenkatesh Pallipadi /* 1166eed9404SViresh Kumar * rwsem to guarantee that cpufreq driver module doesn't unload during critical 1176eed9404SViresh Kumar * sections 1186eed9404SViresh Kumar */ 1196eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem); 1206eed9404SViresh Kumar 1211da177e4SLinus Torvalds /* internal prototypes */ 12229464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy, 12329464f28SDave Jones unsigned int event); 124d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 12565f27f38SDavid Howells static void handle_update(struct work_struct *work); 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds /** 1281da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1291da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1301da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1311da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1321da177e4SLinus Torvalds * The mutex locks both lists. 1331da177e4SLinus Torvalds */ 134e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 135b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1361da177e4SLinus Torvalds 13774212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 138b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 139b4dfdbb3SAlan Stern { 140b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 14174212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 142b4dfdbb3SAlan Stern return 0; 143b4dfdbb3SAlan Stern } 144b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1451da177e4SLinus Torvalds 146a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 147da584455SViresh Kumar static int cpufreq_disabled(void) 148a7b422cdSKonrad Rzeszutek Wilk { 149a7b422cdSKonrad Rzeszutek Wilk return off; 150a7b422cdSKonrad Rzeszutek Wilk } 151a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 152a7b422cdSKonrad Rzeszutek Wilk { 153a7b422cdSKonrad Rzeszutek Wilk off = 1; 154a7b422cdSKonrad Rzeszutek Wilk } 1553fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1561da177e4SLinus Torvalds 1574d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1584d5dcc42SViresh Kumar { 1590b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1604d5dcc42SViresh Kumar } 1613f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1624d5dcc42SViresh Kumar 163944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 164944e9a03SViresh Kumar { 165944e9a03SViresh Kumar if (have_governor_per_policy()) 166944e9a03SViresh Kumar return &policy->kobj; 167944e9a03SViresh Kumar else 168944e9a03SViresh Kumar return cpufreq_global_kobject; 169944e9a03SViresh Kumar } 170944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 171944e9a03SViresh Kumar 17272a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 17372a4ce34SViresh Kumar { 17472a4ce34SViresh Kumar u64 idle_time; 17572a4ce34SViresh Kumar u64 cur_wall_time; 17672a4ce34SViresh Kumar u64 busy_time; 17772a4ce34SViresh Kumar 17872a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 17972a4ce34SViresh Kumar 18072a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 18172a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 18272a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 18372a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 18472a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 18572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 18672a4ce34SViresh Kumar 18772a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 18872a4ce34SViresh Kumar if (wall) 18972a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 19072a4ce34SViresh Kumar 19172a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 19272a4ce34SViresh Kumar } 19372a4ce34SViresh Kumar 19472a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 19572a4ce34SViresh Kumar { 19672a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 19772a4ce34SViresh Kumar 19872a4ce34SViresh Kumar if (idle_time == -1ULL) 19972a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 20072a4ce34SViresh Kumar else if (!io_busy) 20172a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 20272a4ce34SViresh Kumar 20372a4ce34SViresh Kumar return idle_time; 20472a4ce34SViresh Kumar } 20572a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 20672a4ce34SViresh Kumar 20770e9e778SViresh Kumar /* 20870e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 20970e9e778SViresh Kumar * drivers of SMP systems. It will do following: 21070e9e778SViresh Kumar * - validate & show freq table passed 21170e9e778SViresh Kumar * - set policies transition latency 21270e9e778SViresh Kumar * - policy->cpus with all possible CPUs 21370e9e778SViresh Kumar */ 21470e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 21570e9e778SViresh Kumar struct cpufreq_frequency_table *table, 21670e9e778SViresh Kumar unsigned int transition_latency) 21770e9e778SViresh Kumar { 21870e9e778SViresh Kumar int ret; 21970e9e778SViresh Kumar 22070e9e778SViresh Kumar ret = cpufreq_table_validate_and_show(policy, table); 22170e9e778SViresh Kumar if (ret) { 22270e9e778SViresh Kumar pr_err("%s: invalid frequency table: %d\n", __func__, ret); 22370e9e778SViresh Kumar return ret; 22470e9e778SViresh Kumar } 22570e9e778SViresh Kumar 22670e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 22770e9e778SViresh Kumar 22870e9e778SViresh Kumar /* 229*58405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 23070e9e778SViresh Kumar * share the clock and voltage and clock. 23170e9e778SViresh Kumar */ 23270e9e778SViresh Kumar cpumask_setall(policy->cpus); 23370e9e778SViresh Kumar 23470e9e778SViresh Kumar return 0; 23570e9e778SViresh Kumar } 23670e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 23770e9e778SViresh Kumar 238988bed09SViresh Kumar /* Only for cpufreq core internal use */ 239988bed09SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 240652ed95dSViresh Kumar { 241652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 242652ed95dSViresh Kumar 243988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 244988bed09SViresh Kumar } 245988bed09SViresh Kumar 246988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 247988bed09SViresh Kumar { 248988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 249988bed09SViresh Kumar 250652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 251e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 252e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 253652ed95dSViresh Kumar return 0; 254652ed95dSViresh Kumar } 255652ed95dSViresh Kumar 256652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 257652ed95dSViresh Kumar } 258652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 259652ed95dSViresh Kumar 26050e9c852SViresh Kumar /** 26150e9c852SViresh Kumar * cpufreq_cpu_get: returns policy for a cpu and marks it busy. 26250e9c852SViresh Kumar * 26350e9c852SViresh Kumar * @cpu: cpu to find policy for. 26450e9c852SViresh Kumar * 26550e9c852SViresh Kumar * This returns policy for 'cpu', returns NULL if it doesn't exist. 26650e9c852SViresh Kumar * It also increments the kobject reference count to mark it busy and so would 26750e9c852SViresh Kumar * require a corresponding call to cpufreq_cpu_put() to decrement it back. 26850e9c852SViresh Kumar * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be 26950e9c852SViresh Kumar * freed as that depends on the kobj count. 27050e9c852SViresh Kumar * 27150e9c852SViresh Kumar * It also takes a read-lock of 'cpufreq_rwsem' and doesn't put it back if a 27250e9c852SViresh Kumar * valid policy is found. This is done to make sure the driver doesn't get 27350e9c852SViresh Kumar * unregistered while the policy is being used. 27450e9c852SViresh Kumar * 27550e9c852SViresh Kumar * Return: A valid policy on success, otherwise NULL on failure. 27650e9c852SViresh Kumar */ 2776eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2781da177e4SLinus Torvalds { 2796eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2801da177e4SLinus Torvalds unsigned long flags; 2811da177e4SLinus Torvalds 2821b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2836eed9404SViresh Kumar return NULL; 2846eed9404SViresh Kumar 2856eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 2866eed9404SViresh Kumar return NULL; 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds /* get the cpufreq driver */ 2890d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2901da177e4SLinus Torvalds 2916eed9404SViresh Kumar if (cpufreq_driver) { 2921da177e4SLinus Torvalds /* get the CPU */ 293988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2946eed9404SViresh Kumar if (policy) 2956eed9404SViresh Kumar kobject_get(&policy->kobj); 2966eed9404SViresh Kumar } 2976eed9404SViresh Kumar 2986eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2991da177e4SLinus Torvalds 3003a3e9e06SViresh Kumar if (!policy) 3016eed9404SViresh Kumar up_read(&cpufreq_rwsem); 3021da177e4SLinus Torvalds 3033a3e9e06SViresh Kumar return policy; 304a9144436SStephen Boyd } 3051da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 3061da177e4SLinus Torvalds 30750e9c852SViresh Kumar /** 30850e9c852SViresh Kumar * cpufreq_cpu_put: Decrements the usage count of a policy 30950e9c852SViresh Kumar * 31050e9c852SViresh Kumar * @policy: policy earlier returned by cpufreq_cpu_get(). 31150e9c852SViresh Kumar * 31250e9c852SViresh Kumar * This decrements the kobject reference count incremented earlier by calling 31350e9c852SViresh Kumar * cpufreq_cpu_get(). 31450e9c852SViresh Kumar * 31550e9c852SViresh Kumar * It also drops the read-lock of 'cpufreq_rwsem' taken at cpufreq_cpu_get(). 31650e9c852SViresh Kumar */ 3173a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 318a9144436SStephen Boyd { 3196eed9404SViresh Kumar kobject_put(&policy->kobj); 3206eed9404SViresh Kumar up_read(&cpufreq_rwsem); 321a9144436SStephen Boyd } 3221da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /********************************************************************* 3251da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 3261da177e4SLinus Torvalds *********************************************************************/ 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /** 3291da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 3301da177e4SLinus Torvalds * 3311da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 3321da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 3331da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 3341da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 3351da177e4SLinus Torvalds */ 33639c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 33739c132eeSViresh Kumar { 3381da177e4SLinus Torvalds #ifndef CONFIG_SMP 3391da177e4SLinus Torvalds static unsigned long l_p_j_ref; 3401da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 3431da177e4SLinus Torvalds return; 3441da177e4SLinus Torvalds 3451da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 3461da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 3471da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 348e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 349e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 3501da177e4SLinus Torvalds } 3510b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 352e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 353e08f5f5bSGautham R Shenoy ci->new); 354e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 355e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3561da177e4SLinus Torvalds } 3571da177e4SLinus Torvalds #endif 35839c132eeSViresh Kumar } 3591da177e4SLinus Torvalds 3600956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 361b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 3621da177e4SLinus Torvalds { 3631da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3641da177e4SLinus Torvalds 365d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 366d5aaffa9SDirk Brandewie return; 367d5aaffa9SDirk Brandewie 3681c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3692d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 370e4472cb3SDave Jones state, freqs->new); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds switch (state) { 373e4472cb3SDave Jones 3741da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 375e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 376e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 377e4472cb3SDave Jones * "old frequency". 3781da177e4SLinus Torvalds */ 3791c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 380e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 381e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 382e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 383e4472cb3SDave Jones freqs->old, policy->cur); 384e4472cb3SDave Jones freqs->old = policy->cur; 3851da177e4SLinus Torvalds } 3861da177e4SLinus Torvalds } 387b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 388e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3891da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3901da177e4SLinus Torvalds break; 391e4472cb3SDave Jones 3921da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3931da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 394e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 395e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 39625e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 397b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 398e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 399e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 400e4472cb3SDave Jones policy->cur = freqs->new; 4011da177e4SLinus Torvalds break; 4021da177e4SLinus Torvalds } 4031da177e4SLinus Torvalds } 404bb176f7dSViresh Kumar 405b43a7ffbSViresh Kumar /** 406b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 407b43a7ffbSViresh Kumar * on frequency transition. 408b43a7ffbSViresh Kumar * 409b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 410b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 411b43a7ffbSViresh Kumar * external effects. 412b43a7ffbSViresh Kumar */ 413236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 414b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 415b43a7ffbSViresh Kumar { 416b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 417b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 418b43a7ffbSViresh Kumar } 4191da177e4SLinus Torvalds 420f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 421236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 422f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 423f7ba3b41SViresh Kumar { 424f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 425f7ba3b41SViresh Kumar if (!transition_failed) 426f7ba3b41SViresh Kumar return; 427f7ba3b41SViresh Kumar 428f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 429f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 430f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 431f7ba3b41SViresh Kumar } 432f7ba3b41SViresh Kumar 43312478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 43412478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 43512478cf0SSrivatsa S. Bhat { 436ca654dc3SSrivatsa S. Bhat 437ca654dc3SSrivatsa S. Bhat /* 438ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 439ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 440ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 441ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 442ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 443ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 444ca654dc3SSrivatsa S. Bhat */ 445ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 446ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 447ca654dc3SSrivatsa S. Bhat 44812478cf0SSrivatsa S. Bhat wait: 44912478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 45012478cf0SSrivatsa S. Bhat 45112478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 45212478cf0SSrivatsa S. Bhat 45312478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 45412478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 45512478cf0SSrivatsa S. Bhat goto wait; 45612478cf0SSrivatsa S. Bhat } 45712478cf0SSrivatsa S. Bhat 45812478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 459ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 46012478cf0SSrivatsa S. Bhat 46112478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 46212478cf0SSrivatsa S. Bhat 46312478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 46412478cf0SSrivatsa S. Bhat } 46512478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 46612478cf0SSrivatsa S. Bhat 46712478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 46812478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 46912478cf0SSrivatsa S. Bhat { 47012478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 47112478cf0SSrivatsa S. Bhat return; 47212478cf0SSrivatsa S. Bhat 47312478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 47412478cf0SSrivatsa S. Bhat 47512478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 476ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 47712478cf0SSrivatsa S. Bhat 47812478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 47912478cf0SSrivatsa S. Bhat } 48012478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 48112478cf0SSrivatsa S. Bhat 4821da177e4SLinus Torvalds 4831da177e4SLinus Torvalds /********************************************************************* 4841da177e4SLinus Torvalds * SYSFS INTERFACE * 4851da177e4SLinus Torvalds *********************************************************************/ 4868a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 4876f19efc0SLukasz Majewski struct attribute *attr, char *buf) 4886f19efc0SLukasz Majewski { 4896f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 4906f19efc0SLukasz Majewski } 4916f19efc0SLukasz Majewski 4926f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 4936f19efc0SLukasz Majewski const char *buf, size_t count) 4946f19efc0SLukasz Majewski { 4956f19efc0SLukasz Majewski int ret, enable; 4966f19efc0SLukasz Majewski 4976f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 4986f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 4996f19efc0SLukasz Majewski return -EINVAL; 5006f19efc0SLukasz Majewski 5016f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 502e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 503e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 5046f19efc0SLukasz Majewski return -EINVAL; 5056f19efc0SLukasz Majewski } 5066f19efc0SLukasz Majewski 507e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 508e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 5096f19efc0SLukasz Majewski 5106f19efc0SLukasz Majewski return count; 5116f19efc0SLukasz Majewski } 5126f19efc0SLukasz Majewski define_one_global_rw(boost); 5131da177e4SLinus Torvalds 51442f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 5153bcb09a3SJeremy Fitzhardinge { 5163bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 5173bcb09a3SJeremy Fitzhardinge 518f7b27061SViresh Kumar for_each_governor(t) 5197c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 5203bcb09a3SJeremy Fitzhardinge return t; 5213bcb09a3SJeremy Fitzhardinge 5223bcb09a3SJeremy Fitzhardinge return NULL; 5233bcb09a3SJeremy Fitzhardinge } 5243bcb09a3SJeremy Fitzhardinge 5251da177e4SLinus Torvalds /** 5261da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 5271da177e4SLinus Torvalds */ 5281da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 5291da177e4SLinus Torvalds struct cpufreq_governor **governor) 5301da177e4SLinus Torvalds { 5313bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 5323bcb09a3SJeremy Fitzhardinge 5331c3d85ddSRafael J. Wysocki if (!cpufreq_driver) 5343bcb09a3SJeremy Fitzhardinge goto out; 5353bcb09a3SJeremy Fitzhardinge 5361c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 5377c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 5381da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 5393bcb09a3SJeremy Fitzhardinge err = 0; 5407c4f4539SRasmus Villemoes } else if (!strncasecmp(str_governor, "powersave", 541e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 5421da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 5433bcb09a3SJeremy Fitzhardinge err = 0; 5441da177e4SLinus Torvalds } 5452e1cc3a5SViresh Kumar } else { 5461da177e4SLinus Torvalds struct cpufreq_governor *t; 5473bcb09a3SJeremy Fitzhardinge 5483fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 5493bcb09a3SJeremy Fitzhardinge 55042f91fa1SViresh Kumar t = find_governor(str_governor); 5513bcb09a3SJeremy Fitzhardinge 552ea714970SJeremy Fitzhardinge if (t == NULL) { 553ea714970SJeremy Fitzhardinge int ret; 554ea714970SJeremy Fitzhardinge 555ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5561a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 557ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 558ea714970SJeremy Fitzhardinge 559ea714970SJeremy Fitzhardinge if (ret == 0) 56042f91fa1SViresh Kumar t = find_governor(str_governor); 561ea714970SJeremy Fitzhardinge } 562ea714970SJeremy Fitzhardinge 5633bcb09a3SJeremy Fitzhardinge if (t != NULL) { 5641da177e4SLinus Torvalds *governor = t; 5653bcb09a3SJeremy Fitzhardinge err = 0; 5661da177e4SLinus Torvalds } 5673bcb09a3SJeremy Fitzhardinge 5683bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds out: 5713bcb09a3SJeremy Fitzhardinge return err; 5721da177e4SLinus Torvalds } 5731da177e4SLinus Torvalds 5741da177e4SLinus Torvalds /** 575e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 576e08f5f5bSGautham R Shenoy * print out cpufreq information 5771da177e4SLinus Torvalds * 5781da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 5791da177e4SLinus Torvalds * "unsigned int". 5801da177e4SLinus Torvalds */ 5811da177e4SLinus Torvalds 5821da177e4SLinus Torvalds #define show_one(file_name, object) \ 5831da177e4SLinus Torvalds static ssize_t show_##file_name \ 5841da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 5851da177e4SLinus Torvalds { \ 5861da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 5871da177e4SLinus Torvalds } 5881da177e4SLinus Torvalds 5891da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 5901da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 591ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 5921da177e4SLinus Torvalds show_one(scaling_min_freq, min); 5931da177e4SLinus Torvalds show_one(scaling_max_freq, max); 594c034b02eSDirk Brandewie 59509347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 596c034b02eSDirk Brandewie { 597c034b02eSDirk Brandewie ssize_t ret; 598c034b02eSDirk Brandewie 599c034b02eSDirk Brandewie if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 600c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 601c034b02eSDirk Brandewie else 602c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 603c034b02eSDirk Brandewie return ret; 604c034b02eSDirk Brandewie } 6051da177e4SLinus Torvalds 606037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 6073a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 6087970e08bSThomas Renninger 6091da177e4SLinus Torvalds /** 6101da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 6111da177e4SLinus Torvalds */ 6121da177e4SLinus Torvalds #define store_one(file_name, object) \ 6131da177e4SLinus Torvalds static ssize_t store_##file_name \ 6141da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 6151da177e4SLinus Torvalds { \ 616619c144cSVince Hsu int ret, temp; \ 6171da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 6181da177e4SLinus Torvalds \ 6191da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 6201da177e4SLinus Torvalds if (ret) \ 6211da177e4SLinus Torvalds return -EINVAL; \ 6221da177e4SLinus Torvalds \ 6231da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 6241da177e4SLinus Torvalds if (ret != 1) \ 6251da177e4SLinus Torvalds return -EINVAL; \ 6261da177e4SLinus Torvalds \ 627619c144cSVince Hsu temp = new_policy.object; \ 628037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 629619c144cSVince Hsu if (!ret) \ 630619c144cSVince Hsu policy->user_policy.object = temp; \ 6311da177e4SLinus Torvalds \ 6321da177e4SLinus Torvalds return ret ? ret : count; \ 6331da177e4SLinus Torvalds } 6341da177e4SLinus Torvalds 6351da177e4SLinus Torvalds store_one(scaling_min_freq, min); 6361da177e4SLinus Torvalds store_one(scaling_max_freq, max); 6371da177e4SLinus Torvalds 6381da177e4SLinus Torvalds /** 6391da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 6401da177e4SLinus Torvalds */ 641e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 642e08f5f5bSGautham R Shenoy char *buf) 6431da177e4SLinus Torvalds { 644d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 6451da177e4SLinus Torvalds if (!cur_freq) 6461da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 6471da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 6481da177e4SLinus Torvalds } 6491da177e4SLinus Torvalds 6501da177e4SLinus Torvalds /** 6511da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 6521da177e4SLinus Torvalds */ 653905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 6541da177e4SLinus Torvalds { 6551da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 6561da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 6571da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 6581da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 6591da177e4SLinus Torvalds else if (policy->governor) 6604b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 66129464f28SDave Jones policy->governor->name); 6621da177e4SLinus Torvalds return -EINVAL; 6631da177e4SLinus Torvalds } 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds /** 6661da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 6671da177e4SLinus Torvalds */ 6681da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 6691da177e4SLinus Torvalds const char *buf, size_t count) 6701da177e4SLinus Torvalds { 6715136fa56SSrivatsa S. Bhat int ret; 6721da177e4SLinus Torvalds char str_governor[16]; 6731da177e4SLinus Torvalds struct cpufreq_policy new_policy; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 6761da177e4SLinus Torvalds if (ret) 6771da177e4SLinus Torvalds return ret; 6781da177e4SLinus Torvalds 6791da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 6801da177e4SLinus Torvalds if (ret != 1) 6811da177e4SLinus Torvalds return -EINVAL; 6821da177e4SLinus Torvalds 683e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 684e08f5f5bSGautham R Shenoy &new_policy.governor)) 6851da177e4SLinus Torvalds return -EINVAL; 6861da177e4SLinus Torvalds 687037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 6887970e08bSThomas Renninger 6897970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 6907970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 6917970e08bSThomas Renninger 692e08f5f5bSGautham R Shenoy if (ret) 693e08f5f5bSGautham R Shenoy return ret; 694e08f5f5bSGautham R Shenoy else 695e08f5f5bSGautham R Shenoy return count; 6961da177e4SLinus Torvalds } 6971da177e4SLinus Torvalds 6981da177e4SLinus Torvalds /** 6991da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 7001da177e4SLinus Torvalds */ 7011da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 7021da177e4SLinus Torvalds { 7031c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 7041da177e4SLinus Torvalds } 7051da177e4SLinus Torvalds 7061da177e4SLinus Torvalds /** 7071da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 7081da177e4SLinus Torvalds */ 7091da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 7101da177e4SLinus Torvalds char *buf) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds ssize_t i = 0; 7131da177e4SLinus Torvalds struct cpufreq_governor *t; 7141da177e4SLinus Torvalds 7159c0ebcf7SViresh Kumar if (!has_target()) { 7161da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 7171da177e4SLinus Torvalds goto out; 7181da177e4SLinus Torvalds } 7191da177e4SLinus Torvalds 720f7b27061SViresh Kumar for_each_governor(t) { 72129464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 72229464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 7231da177e4SLinus Torvalds goto out; 7244b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 7251da177e4SLinus Torvalds } 7261da177e4SLinus Torvalds out: 7271da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 7281da177e4SLinus Torvalds return i; 7291da177e4SLinus Torvalds } 730e8628dd0SDarrick J. Wong 731f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 7321da177e4SLinus Torvalds { 7331da177e4SLinus Torvalds ssize_t i = 0; 7341da177e4SLinus Torvalds unsigned int cpu; 7351da177e4SLinus Torvalds 736835481d9SRusty Russell for_each_cpu(cpu, mask) { 7371da177e4SLinus Torvalds if (i) 7381da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 7391da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 7401da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 7411da177e4SLinus Torvalds break; 7421da177e4SLinus Torvalds } 7431da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 7441da177e4SLinus Torvalds return i; 7451da177e4SLinus Torvalds } 746f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 7471da177e4SLinus Torvalds 748e8628dd0SDarrick J. Wong /** 749e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 750e8628dd0SDarrick J. Wong * hw coordination is in use 751e8628dd0SDarrick J. Wong */ 752e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 753e8628dd0SDarrick J. Wong { 754f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 755e8628dd0SDarrick J. Wong } 756e8628dd0SDarrick J. Wong 757e8628dd0SDarrick J. Wong /** 758e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 759e8628dd0SDarrick J. Wong */ 760e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 761e8628dd0SDarrick J. Wong { 762f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 763e8628dd0SDarrick J. Wong } 764e8628dd0SDarrick J. Wong 7659e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 7669e76988eSVenki Pallipadi const char *buf, size_t count) 7679e76988eSVenki Pallipadi { 7689e76988eSVenki Pallipadi unsigned int freq = 0; 7699e76988eSVenki Pallipadi unsigned int ret; 7709e76988eSVenki Pallipadi 771879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 7729e76988eSVenki Pallipadi return -EINVAL; 7739e76988eSVenki Pallipadi 7749e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 7759e76988eSVenki Pallipadi if (ret != 1) 7769e76988eSVenki Pallipadi return -EINVAL; 7779e76988eSVenki Pallipadi 7789e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 7799e76988eSVenki Pallipadi 7809e76988eSVenki Pallipadi return count; 7819e76988eSVenki Pallipadi } 7829e76988eSVenki Pallipadi 7839e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 7849e76988eSVenki Pallipadi { 785879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 7869e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 7879e76988eSVenki Pallipadi 7889e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 7899e76988eSVenki Pallipadi } 7901da177e4SLinus Torvalds 791e2f74f35SThomas Renninger /** 7928bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 793e2f74f35SThomas Renninger */ 794e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 795e2f74f35SThomas Renninger { 796e2f74f35SThomas Renninger unsigned int limit; 797e2f74f35SThomas Renninger int ret; 7981c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 7991c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 800e2f74f35SThomas Renninger if (!ret) 801e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 802e2f74f35SThomas Renninger } 803e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 804e2f74f35SThomas Renninger } 805e2f74f35SThomas Renninger 8066dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 8076dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 8086dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 8096dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 8106dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 8116dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 8126dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 8136dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 8146dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 8156dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 8166dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 8176dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 8186dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 8196dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 8201da177e4SLinus Torvalds 8211da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 8221da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 8231da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 824ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 8251da177e4SLinus Torvalds &scaling_min_freq.attr, 8261da177e4SLinus Torvalds &scaling_max_freq.attr, 8271da177e4SLinus Torvalds &affected_cpus.attr, 828e8628dd0SDarrick J. Wong &related_cpus.attr, 8291da177e4SLinus Torvalds &scaling_governor.attr, 8301da177e4SLinus Torvalds &scaling_driver.attr, 8311da177e4SLinus Torvalds &scaling_available_governors.attr, 8329e76988eSVenki Pallipadi &scaling_setspeed.attr, 8331da177e4SLinus Torvalds NULL 8341da177e4SLinus Torvalds }; 8351da177e4SLinus Torvalds 8361da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 8371da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 8381da177e4SLinus Torvalds 8391da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 8401da177e4SLinus Torvalds { 8411da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8421da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 8431b750e3bSViresh Kumar ssize_t ret; 8446eed9404SViresh Kumar 8456eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 8461b750e3bSViresh Kumar return -EINVAL; 8475a01f2e8SVenkatesh Pallipadi 848ad7722daSviresh kumar down_read(&policy->rwsem); 8495a01f2e8SVenkatesh Pallipadi 850e08f5f5bSGautham R Shenoy if (fattr->show) 851e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 852e08f5f5bSGautham R Shenoy else 853e08f5f5bSGautham R Shenoy ret = -EIO; 854e08f5f5bSGautham R Shenoy 855ad7722daSviresh kumar up_read(&policy->rwsem); 8566eed9404SViresh Kumar up_read(&cpufreq_rwsem); 8571b750e3bSViresh Kumar 8581da177e4SLinus Torvalds return ret; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 8621da177e4SLinus Torvalds const char *buf, size_t count) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8651da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 866a07530b4SDave Jones ssize_t ret = -EINVAL; 8676eed9404SViresh Kumar 8684f750c93SSrivatsa S. Bhat get_online_cpus(); 8694f750c93SSrivatsa S. Bhat 8704f750c93SSrivatsa S. Bhat if (!cpu_online(policy->cpu)) 8714f750c93SSrivatsa S. Bhat goto unlock; 8724f750c93SSrivatsa S. Bhat 8736eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 8744f750c93SSrivatsa S. Bhat goto unlock; 8755a01f2e8SVenkatesh Pallipadi 876ad7722daSviresh kumar down_write(&policy->rwsem); 8775a01f2e8SVenkatesh Pallipadi 878e08f5f5bSGautham R Shenoy if (fattr->store) 879e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 880e08f5f5bSGautham R Shenoy else 881e08f5f5bSGautham R Shenoy ret = -EIO; 882e08f5f5bSGautham R Shenoy 883ad7722daSviresh kumar up_write(&policy->rwsem); 8846eed9404SViresh Kumar 8856eed9404SViresh Kumar up_read(&cpufreq_rwsem); 8864f750c93SSrivatsa S. Bhat unlock: 8874f750c93SSrivatsa S. Bhat put_online_cpus(); 8884f750c93SSrivatsa S. Bhat 8891da177e4SLinus Torvalds return ret; 8901da177e4SLinus Torvalds } 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 8931da177e4SLinus Torvalds { 8941da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8952d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 8961da177e4SLinus Torvalds complete(&policy->kobj_unregister); 8971da177e4SLinus Torvalds } 8981da177e4SLinus Torvalds 89952cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 9001da177e4SLinus Torvalds .show = show, 9011da177e4SLinus Torvalds .store = store, 9021da177e4SLinus Torvalds }; 9031da177e4SLinus Torvalds 9041da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 9051da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 9061da177e4SLinus Torvalds .default_attrs = default_attrs, 9071da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 9081da177e4SLinus Torvalds }; 9091da177e4SLinus Torvalds 9102361be23SViresh Kumar struct kobject *cpufreq_global_kobject; 9112361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 9122361be23SViresh Kumar 9132361be23SViresh Kumar static int cpufreq_global_kobject_usage; 9142361be23SViresh Kumar 9152361be23SViresh Kumar int cpufreq_get_global_kobject(void) 9162361be23SViresh Kumar { 9172361be23SViresh Kumar if (!cpufreq_global_kobject_usage++) 9182361be23SViresh Kumar return kobject_add(cpufreq_global_kobject, 9192361be23SViresh Kumar &cpu_subsys.dev_root->kobj, "%s", "cpufreq"); 9202361be23SViresh Kumar 9212361be23SViresh Kumar return 0; 9222361be23SViresh Kumar } 9232361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject); 9242361be23SViresh Kumar 9252361be23SViresh Kumar void cpufreq_put_global_kobject(void) 9262361be23SViresh Kumar { 9272361be23SViresh Kumar if (!--cpufreq_global_kobject_usage) 9282361be23SViresh Kumar kobject_del(cpufreq_global_kobject); 9292361be23SViresh Kumar } 9302361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject); 9312361be23SViresh Kumar 9322361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr) 9332361be23SViresh Kumar { 9342361be23SViresh Kumar int ret = cpufreq_get_global_kobject(); 9352361be23SViresh Kumar 9362361be23SViresh Kumar if (!ret) { 9372361be23SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, attr); 9382361be23SViresh Kumar if (ret) 9392361be23SViresh Kumar cpufreq_put_global_kobject(); 9402361be23SViresh Kumar } 9412361be23SViresh Kumar 9422361be23SViresh Kumar return ret; 9432361be23SViresh Kumar } 9442361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file); 9452361be23SViresh Kumar 9462361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr) 9472361be23SViresh Kumar { 9482361be23SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, attr); 9492361be23SViresh Kumar cpufreq_put_global_kobject(); 9502361be23SViresh Kumar } 9512361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file); 9522361be23SViresh Kumar 95319d6f7ecSDave Jones /* symlink affected CPUs */ 954308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 95519d6f7ecSDave Jones { 95619d6f7ecSDave Jones unsigned int j; 95719d6f7ecSDave Jones int ret = 0; 95819d6f7ecSDave Jones 95919d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 9608a25a2fdSKay Sievers struct device *cpu_dev; 96119d6f7ecSDave Jones 962308b60e7SViresh Kumar if (j == policy->cpu) 96319d6f7ecSDave Jones continue; 96419d6f7ecSDave Jones 965e8fdde10SViresh Kumar pr_debug("Adding link for CPU: %u\n", j); 9668a25a2fdSKay Sievers cpu_dev = get_cpu_device(j); 9678a25a2fdSKay Sievers ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 96819d6f7ecSDave Jones "cpufreq"); 96971c3461eSRafael J. Wysocki if (ret) 97071c3461eSRafael J. Wysocki break; 97119d6f7ecSDave Jones } 97219d6f7ecSDave Jones return ret; 97319d6f7ecSDave Jones } 97419d6f7ecSDave Jones 975308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy, 9768a25a2fdSKay Sievers struct device *dev) 977909a694eSDave Jones { 978909a694eSDave Jones struct freq_attr **drv_attr; 979909a694eSDave Jones int ret = 0; 980909a694eSDave Jones 981909a694eSDave Jones /* set up files for this cpu device */ 9821c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 983f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 984909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 985909a694eSDave Jones if (ret) 9866d4e81edSTomeu Vizoso return ret; 987909a694eSDave Jones drv_attr++; 988909a694eSDave Jones } 9891c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 990909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 991909a694eSDave Jones if (ret) 9926d4e81edSTomeu Vizoso return ret; 993909a694eSDave Jones } 994c034b02eSDirk Brandewie 995909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 996909a694eSDave Jones if (ret) 9976d4e81edSTomeu Vizoso return ret; 998c034b02eSDirk Brandewie 9991c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 1000e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 1001e2f74f35SThomas Renninger if (ret) 10026d4e81edSTomeu Vizoso return ret; 1003e2f74f35SThomas Renninger } 1004909a694eSDave Jones 10056d4e81edSTomeu Vizoso return cpufreq_add_dev_symlink(policy); 1006e18f1682SSrivatsa S. Bhat } 1007e18f1682SSrivatsa S. Bhat 1008e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy) 1009e18f1682SSrivatsa S. Bhat { 10106e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 1011e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 1012e18f1682SSrivatsa S. Bhat int ret = 0; 1013e18f1682SSrivatsa S. Bhat 1014d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 1015a27a9ab7SJason Baron 10166e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 10174573237bSViresh Kumar gov = find_governor(policy->last_governor); 10186e2c89d1Sviresh kumar if (gov) 10196e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 10206e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 10216e2c89d1Sviresh kumar else 10226e2c89d1Sviresh kumar gov = CPUFREQ_DEFAULT_GOVERNOR; 10236e2c89d1Sviresh kumar 10246e2c89d1Sviresh kumar new_policy.governor = gov; 10256e2c89d1Sviresh kumar 1026a27a9ab7SJason Baron /* Use the default policy if its valid. */ 1027a27a9ab7SJason Baron if (cpufreq_driver->setpolicy) 10286e2c89d1Sviresh kumar cpufreq_parse_governor(gov->name, &new_policy.policy, NULL); 1029ecf7e461SDave Jones 1030ecf7e461SDave Jones /* set default policy */ 1031037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 1032ecf7e461SDave Jones if (ret) { 10332d06d8c4SDominik Brodowski pr_debug("setting policy failed\n"); 10341c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 10351c3d85ddSRafael J. Wysocki cpufreq_driver->exit(policy); 1036ecf7e461SDave Jones } 1037909a694eSDave Jones } 1038909a694eSDave Jones 1039d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, 104042f921a6SViresh Kumar unsigned int cpu, struct device *dev) 1041fcf80582SViresh Kumar { 10429c0ebcf7SViresh Kumar int ret = 0; 1043fcf80582SViresh Kumar 1044bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 1045bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 1046bb29ae15SViresh Kumar return 0; 1047bb29ae15SViresh Kumar 10489c0ebcf7SViresh Kumar if (has_target()) { 10493de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 10503de9bdebSViresh Kumar if (ret) { 10513de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 10523de9bdebSViresh Kumar return ret; 10533de9bdebSViresh Kumar } 10543de9bdebSViresh Kumar } 1055fcf80582SViresh Kumar 1056ad7722daSviresh kumar down_write(&policy->rwsem); 1057fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 1058ad7722daSviresh kumar up_write(&policy->rwsem); 10592eaa3e2dSViresh Kumar 10609c0ebcf7SViresh Kumar if (has_target()) { 1061e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1062e5c87b76SStratos Karafotis if (!ret) 1063e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1064e5c87b76SStratos Karafotis 1065e5c87b76SStratos Karafotis if (ret) { 10663de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 10673de9bdebSViresh Kumar return ret; 10683de9bdebSViresh Kumar } 1069820c6ca2SViresh Kumar } 1070fcf80582SViresh Kumar 107142f921a6SViresh Kumar return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"); 1072fcf80582SViresh Kumar } 10731da177e4SLinus Torvalds 10748414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu) 10758414809cSSrivatsa S. Bhat { 10768414809cSSrivatsa S. Bhat struct cpufreq_policy *policy; 10778414809cSSrivatsa S. Bhat unsigned long flags; 10788414809cSSrivatsa S. Bhat 107944871c9cSLan Tianyu read_lock_irqsave(&cpufreq_driver_lock, flags); 10803914d379SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 108144871c9cSLan Tianyu read_unlock_irqrestore(&cpufreq_driver_lock, flags); 10828414809cSSrivatsa S. Bhat 10833914d379SViresh Kumar if (likely(policy)) { 10843914d379SViresh Kumar /* Policy should be inactive here */ 10853914d379SViresh Kumar WARN_ON(!policy_is_inactive(policy)); 10863914d379SViresh Kumar } 10876e2c89d1Sviresh kumar 10888414809cSSrivatsa S. Bhat return policy; 10898414809cSSrivatsa S. Bhat } 10908414809cSSrivatsa S. Bhat 1091e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void) 1092e9698cc5SSrivatsa S. Bhat { 1093e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 1094e9698cc5SSrivatsa S. Bhat 1095e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 1096e9698cc5SSrivatsa S. Bhat if (!policy) 1097e9698cc5SSrivatsa S. Bhat return NULL; 1098e9698cc5SSrivatsa S. Bhat 1099e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1100e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1101e9698cc5SSrivatsa S. Bhat 1102e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1103e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1104e9698cc5SSrivatsa S. Bhat 1105c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1106ad7722daSviresh kumar init_rwsem(&policy->rwsem); 110712478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 110812478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1109818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1110818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1111ad7722daSviresh kumar 1112e9698cc5SSrivatsa S. Bhat return policy; 1113e9698cc5SSrivatsa S. Bhat 1114e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1115e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1116e9698cc5SSrivatsa S. Bhat err_free_policy: 1117e9698cc5SSrivatsa S. Bhat kfree(policy); 1118e9698cc5SSrivatsa S. Bhat 1119e9698cc5SSrivatsa S. Bhat return NULL; 1120e9698cc5SSrivatsa S. Bhat } 1121e9698cc5SSrivatsa S. Bhat 112242f921a6SViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy) 112342f921a6SViresh Kumar { 112442f921a6SViresh Kumar struct kobject *kobj; 112542f921a6SViresh Kumar struct completion *cmp; 112642f921a6SViresh Kumar 1127fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1128fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1129fcd7af91SViresh Kumar 113042f921a6SViresh Kumar down_read(&policy->rwsem); 113142f921a6SViresh Kumar kobj = &policy->kobj; 113242f921a6SViresh Kumar cmp = &policy->kobj_unregister; 113342f921a6SViresh Kumar up_read(&policy->rwsem); 113442f921a6SViresh Kumar kobject_put(kobj); 113542f921a6SViresh Kumar 113642f921a6SViresh Kumar /* 113742f921a6SViresh Kumar * We need to make sure that the underlying kobj is 113842f921a6SViresh Kumar * actually not referenced anymore by anybody before we 113942f921a6SViresh Kumar * proceed with unloading. 114042f921a6SViresh Kumar */ 114142f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 114242f921a6SViresh Kumar wait_for_completion(cmp); 114342f921a6SViresh Kumar pr_debug("wait complete\n"); 114442f921a6SViresh Kumar } 114542f921a6SViresh Kumar 1146e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy) 1147e9698cc5SSrivatsa S. Bhat { 1148988bed09SViresh Kumar unsigned long flags; 1149988bed09SViresh Kumar int cpu; 1150988bed09SViresh Kumar 1151988bed09SViresh Kumar /* Remove policy from list */ 1152988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1153988bed09SViresh Kumar list_del(&policy->policy_list); 1154988bed09SViresh Kumar 1155988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1156988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1157988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1158988bed09SViresh Kumar 1159e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1160e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1161e9698cc5SSrivatsa S. Bhat kfree(policy); 1162e9698cc5SSrivatsa S. Bhat } 1163e9698cc5SSrivatsa S. Bhat 11641bfb425bSViresh Kumar static int update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu, 11651bfb425bSViresh Kumar struct device *cpu_dev) 11660d66b91eSSrivatsa S. Bhat { 11671bfb425bSViresh Kumar int ret; 11681bfb425bSViresh Kumar 116999ec899eSSrivatsa S. Bhat if (WARN_ON(cpu == policy->cpu)) 11701bfb425bSViresh Kumar return 0; 11711bfb425bSViresh Kumar 11721bfb425bSViresh Kumar /* Move kobject to the new policy->cpu */ 11731bfb425bSViresh Kumar ret = kobject_move(&policy->kobj, &cpu_dev->kobj); 11741bfb425bSViresh Kumar if (ret) { 11751bfb425bSViresh Kumar pr_err("%s: Failed to move kobj: %d\n", __func__, ret); 11761bfb425bSViresh Kumar return ret; 11771bfb425bSViresh Kumar } 1178cb38ed5cSSrivatsa S. Bhat 1179ad7722daSviresh kumar down_write(&policy->rwsem); 11800d66b91eSSrivatsa S. Bhat policy->cpu = cpu; 1181ad7722daSviresh kumar up_write(&policy->rwsem); 11828efd5765SViresh Kumar 11831bfb425bSViresh Kumar return 0; 11840d66b91eSSrivatsa S. Bhat } 11850d66b91eSSrivatsa S. Bhat 118623faf0b7SViresh Kumar /** 118723faf0b7SViresh Kumar * cpufreq_add_dev - add a CPU device 118823faf0b7SViresh Kumar * 118923faf0b7SViresh Kumar * Adds the cpufreq interface for a CPU device. 119023faf0b7SViresh Kumar * 119123faf0b7SViresh Kumar * The Oracle says: try running cpufreq registration/unregistration concurrently 119223faf0b7SViresh Kumar * with with cpu hotplugging and all hell will break loose. Tried to clean this 119323faf0b7SViresh Kumar * mess up, but more thorough testing is needed. - Mathieu 119423faf0b7SViresh Kumar */ 119523faf0b7SViresh Kumar static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 11961da177e4SLinus Torvalds { 1197fcf80582SViresh Kumar unsigned int j, cpu = dev->id; 119865922465SViresh Kumar int ret = -ENOMEM; 11997f0c020aSViresh Kumar struct cpufreq_policy *policy; 12001da177e4SLinus Torvalds unsigned long flags; 120196bbbe4aSViresh Kumar bool recover_policy = cpufreq_suspended; 12021da177e4SLinus Torvalds 1203c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 1204c32b6b8eSAshok Raj return 0; 1205c32b6b8eSAshok Raj 12062d06d8c4SDominik Brodowski pr_debug("adding CPU %u\n", cpu); 12071da177e4SLinus Torvalds 12086eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 12096eed9404SViresh Kumar return 0; 12106eed9404SViresh Kumar 1211bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 12129104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 12139104bb26SViresh Kumar if (policy && !policy_is_inactive(policy)) { 12149104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 12157f0c020aSViresh Kumar ret = cpufreq_add_policy_cpu(policy, cpu, dev); 12166eed9404SViresh Kumar up_read(&cpufreq_rwsem); 12176eed9404SViresh Kumar return ret; 1218fcf80582SViresh Kumar } 12191da177e4SLinus Torvalds 122072368d12SRafael J. Wysocki /* 122172368d12SRafael J. Wysocki * Restore the saved policy when doing light-weight init and fall back 122272368d12SRafael J. Wysocki * to the full init if that fails. 122372368d12SRafael J. Wysocki */ 122496bbbe4aSViresh Kumar policy = recover_policy ? cpufreq_policy_restore(cpu) : NULL; 122572368d12SRafael J. Wysocki if (!policy) { 122696bbbe4aSViresh Kumar recover_policy = false; 1227e9698cc5SSrivatsa S. Bhat policy = cpufreq_policy_alloc(); 1228059019a3SDave Jones if (!policy) 12291da177e4SLinus Torvalds goto nomem_out; 123072368d12SRafael J. Wysocki } 12310d66b91eSSrivatsa S. Bhat 12320d66b91eSSrivatsa S. Bhat /* 12330d66b91eSSrivatsa S. Bhat * In the resume path, since we restore a saved policy, the assignment 12340d66b91eSSrivatsa S. Bhat * to policy->cpu is like an update of the existing policy, rather than 12350d66b91eSSrivatsa S. Bhat * the creation of a brand new one. So we need to perform this update 12360d66b91eSSrivatsa S. Bhat * by invoking update_policy_cpu(). 12370d66b91eSSrivatsa S. Bhat */ 12381bfb425bSViresh Kumar if (recover_policy && cpu != policy->cpu) 12391bfb425bSViresh Kumar WARN_ON(update_policy_cpu(policy, cpu, dev)); 12401bfb425bSViresh Kumar else 12411da177e4SLinus Torvalds policy->cpu = cpu; 12420d66b91eSSrivatsa S. Bhat 1243835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 12441da177e4SLinus Torvalds 12451da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 12461da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 12471da177e4SLinus Torvalds */ 12481c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 12491da177e4SLinus Torvalds if (ret) { 12502d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 12512eaa3e2dSViresh Kumar goto err_set_policy_cpu; 12521da177e4SLinus Torvalds } 1253643ae6e8SViresh Kumar 12546d4e81edSTomeu Vizoso down_write(&policy->rwsem); 12556d4e81edSTomeu Vizoso 12565a7e56a5SViresh Kumar /* related cpus should atleast have policy->cpus */ 12575a7e56a5SViresh Kumar cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 12585a7e56a5SViresh Kumar 12595a7e56a5SViresh Kumar /* 12605a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 12615a7e56a5SViresh Kumar * managing offline cpus here. 12625a7e56a5SViresh Kumar */ 12635a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 12645a7e56a5SViresh Kumar 126596bbbe4aSViresh Kumar if (!recover_policy) { 12665a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 12675a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 12686d4e81edSTomeu Vizoso 12696d4e81edSTomeu Vizoso /* prepare interface data */ 12706d4e81edSTomeu Vizoso ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 12716d4e81edSTomeu Vizoso &dev->kobj, "cpufreq"); 12726d4e81edSTomeu Vizoso if (ret) { 12736d4e81edSTomeu Vizoso pr_err("%s: failed to init policy->kobj: %d\n", 12746d4e81edSTomeu Vizoso __func__, ret); 12756d4e81edSTomeu Vizoso goto err_init_policy_kobj; 12766d4e81edSTomeu Vizoso } 12775a7e56a5SViresh Kumar 1278652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1279988bed09SViresh Kumar for_each_cpu(j, policy->related_cpus) 1280652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1281652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1282988bed09SViresh Kumar } 1283652ed95dSViresh Kumar 12842ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1285da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1286da60ce9fSViresh Kumar if (!policy->cur) { 1287da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 1288da60ce9fSViresh Kumar goto err_get_freq; 1289da60ce9fSViresh Kumar } 1290da60ce9fSViresh Kumar } 1291da60ce9fSViresh Kumar 1292d3916691SViresh Kumar /* 1293d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1294d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1295d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1296d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1297d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1298d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1299d3916691SViresh Kumar * isn't found in freq-table. 1300d3916691SViresh Kumar * 1301d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1302d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1303d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1304d3916691SViresh Kumar * is initialized to zero). 1305d3916691SViresh Kumar * 1306d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1307d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1308d3916691SViresh Kumar * equal to target-freq. 1309d3916691SViresh Kumar */ 1310d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1311d3916691SViresh Kumar && has_target()) { 1312d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1313d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1314d3916691SViresh Kumar if (ret == -EINVAL) { 1315d3916691SViresh Kumar /* Warn user and fix it */ 1316d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1317d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1318d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1319d3916691SViresh Kumar CPUFREQ_RELATION_L); 1320d3916691SViresh Kumar 1321d3916691SViresh Kumar /* 1322d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1323d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1324d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1325d3916691SViresh Kumar */ 1326d3916691SViresh Kumar BUG_ON(ret); 1327d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1328d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1329d3916691SViresh Kumar } 1330d3916691SViresh Kumar } 1331d3916691SViresh Kumar 1332a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1333a1531acdSThomas Renninger CPUFREQ_START, policy); 1334a1531acdSThomas Renninger 133596bbbe4aSViresh Kumar if (!recover_policy) { 1336308b60e7SViresh Kumar ret = cpufreq_add_dev_interface(policy, dev); 133719d6f7ecSDave Jones if (ret) 13380142f9dcSAhmed S. Darwish goto err_out_unregister; 1339fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1340fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1341c88a1f8bSLukasz Majewski 1342c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1343c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1344c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1345988bed09SViresh Kumar } 13468ff69732SDave Jones 1347e18f1682SSrivatsa S. Bhat cpufreq_init_policy(policy); 1348e18f1682SSrivatsa S. Bhat 134996bbbe4aSViresh Kumar if (!recover_policy) { 135008fd8c1cSViresh Kumar policy->user_policy.policy = policy->policy; 135108fd8c1cSViresh Kumar policy->user_policy.governor = policy->governor; 135208fd8c1cSViresh Kumar } 13534e97b631SViresh Kumar up_write(&policy->rwsem); 135408fd8c1cSViresh Kumar 1355038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 13567c45cf31SViresh Kumar 13576eed9404SViresh Kumar up_read(&cpufreq_rwsem); 13586eed9404SViresh Kumar 13597c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 13607c45cf31SViresh Kumar if (cpufreq_driver->ready) 13617c45cf31SViresh Kumar cpufreq_driver->ready(policy); 13627c45cf31SViresh Kumar 13632d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds return 0; 13661da177e4SLinus Torvalds 13671da177e4SLinus Torvalds err_out_unregister: 1368652ed95dSViresh Kumar err_get_freq: 13696d4e81edSTomeu Vizoso if (!recover_policy) { 13706d4e81edSTomeu Vizoso kobject_put(&policy->kobj); 13716d4e81edSTomeu Vizoso wait_for_completion(&policy->kobj_unregister); 13726d4e81edSTomeu Vizoso } 13736d4e81edSTomeu Vizoso err_init_policy_kobj: 13747106e02bSPrarit Bhargava up_write(&policy->rwsem); 13757106e02bSPrarit Bhargava 1376da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1377da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 13782eaa3e2dSViresh Kumar err_set_policy_cpu: 13793914d379SViresh Kumar if (recover_policy) 138042f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 1381e9698cc5SSrivatsa S. Bhat cpufreq_policy_free(policy); 138242f921a6SViresh Kumar 13831da177e4SLinus Torvalds nomem_out: 13846eed9404SViresh Kumar up_read(&cpufreq_rwsem); 13856eed9404SViresh Kumar 13861da177e4SLinus Torvalds return ret; 13871da177e4SLinus Torvalds } 13881da177e4SLinus Torvalds 1389cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_prepare(struct device *dev, 139096bbbe4aSViresh Kumar struct subsys_interface *sif) 13911da177e4SLinus Torvalds { 1392f9ba680dSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 13931bfb425bSViresh Kumar int ret; 13943a3e9e06SViresh Kumar struct cpufreq_policy *policy; 13951da177e4SLinus Torvalds 1396b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 13971da177e4SLinus Torvalds 1398988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 13993a3e9e06SViresh Kumar if (!policy) { 1400b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 14011da177e4SLinus Torvalds return -EINVAL; 14021da177e4SLinus Torvalds } 14031da177e4SLinus Torvalds 14049c0ebcf7SViresh Kumar if (has_target()) { 14053de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 14063de9bdebSViresh Kumar if (ret) { 14073de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 14083de9bdebSViresh Kumar return ret; 14093de9bdebSViresh Kumar } 1410db5f2995SViresh Kumar } 14111da177e4SLinus Torvalds 14124573237bSViresh Kumar down_write(&policy->rwsem); 14133a3e9e06SViresh Kumar cpus = cpumask_weight(policy->cpus); 14144573237bSViresh Kumar 14154573237bSViresh Kumar if (has_target() && cpus == 1) 14164573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 14174573237bSViresh Kumar CPUFREQ_NAME_LEN); 14184573237bSViresh Kumar up_write(&policy->rwsem); 14191da177e4SLinus Torvalds 142061173f25SSrivatsa S. Bhat if (cpu != policy->cpu) { 142173bf0fc2SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 142273bf0fc2SViresh Kumar } else if (cpus > 1) { 14231bfb425bSViresh Kumar /* Nominate new CPU */ 14241bfb425bSViresh Kumar int new_cpu = cpumask_any_but(policy->cpus, cpu); 14251bfb425bSViresh Kumar struct device *cpu_dev = get_cpu_device(new_cpu); 14261bfb425bSViresh Kumar 14271bfb425bSViresh Kumar sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 14281bfb425bSViresh Kumar ret = update_policy_cpu(policy, new_cpu, cpu_dev); 14291bfb425bSViresh Kumar if (ret) { 14301bfb425bSViresh Kumar if (sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 14311bfb425bSViresh Kumar "cpufreq")) 14321bfb425bSViresh Kumar pr_err("%s: Failed to restore kobj link to cpu:%d\n", 14331bfb425bSViresh Kumar __func__, cpu_dev->id); 14341bfb425bSViresh Kumar return ret; 14351bfb425bSViresh Kumar } 1436a82fab29SSrivatsa S. Bhat 1437bda9f552SStratos Karafotis if (!cpufreq_suspended) 143875949c9aSViresh Kumar pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n", 143975949c9aSViresh Kumar __func__, new_cpu, cpu); 1440789ca243SPreeti U Murthy } else if (cpufreq_driver->stop_cpu) { 1441367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 14421da177e4SLinus Torvalds } 1443b8eed8afSViresh Kumar 1444cedb70afSSrivatsa S. Bhat return 0; 1445cedb70afSSrivatsa S. Bhat } 1446cedb70afSSrivatsa S. Bhat 1447cedb70afSSrivatsa S. Bhat static int __cpufreq_remove_dev_finish(struct device *dev, 144896bbbe4aSViresh Kumar struct subsys_interface *sif) 1449cedb70afSSrivatsa S. Bhat { 1450988bed09SViresh Kumar unsigned int cpu = dev->id; 1451cedb70afSSrivatsa S. Bhat int ret; 1452988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 1453cedb70afSSrivatsa S. Bhat 1454cedb70afSSrivatsa S. Bhat if (!policy) { 1455cedb70afSSrivatsa S. Bhat pr_debug("%s: No cpu_data found\n", __func__); 1456cedb70afSSrivatsa S. Bhat return -EINVAL; 1457cedb70afSSrivatsa S. Bhat } 1458cedb70afSSrivatsa S. Bhat 1459ad7722daSviresh kumar down_write(&policy->rwsem); 14609c8f1ee4SViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 1461ad7722daSviresh kumar up_write(&policy->rwsem); 1462cedb70afSSrivatsa S. Bhat 1463b8eed8afSViresh Kumar /* If cpu is last user of policy, free policy */ 1464988bed09SViresh Kumar if (policy_is_inactive(policy)) { 14659c0ebcf7SViresh Kumar if (has_target()) { 14663de9bdebSViresh Kumar ret = __cpufreq_governor(policy, 14673de9bdebSViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 14683de9bdebSViresh Kumar if (ret) { 14693de9bdebSViresh Kumar pr_err("%s: Failed to exit governor\n", 14703de9bdebSViresh Kumar __func__); 14713de9bdebSViresh Kumar return ret; 14723de9bdebSViresh Kumar } 14733de9bdebSViresh Kumar } 14742a998599SRafael J. Wysocki 147596bbbe4aSViresh Kumar if (!cpufreq_suspended) 147642f921a6SViresh Kumar cpufreq_policy_put_kobj(policy); 14771da177e4SLinus Torvalds 14788414809cSSrivatsa S. Bhat /* 14798414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 14808414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 14818414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 14828414809cSSrivatsa S. Bhat */ 14831c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 14843a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 148527ecddc2SJacob Shin 148696bbbe4aSViresh Kumar if (!cpufreq_suspended) 14873a3e9e06SViresh Kumar cpufreq_policy_free(policy); 1488e5c87b76SStratos Karafotis } else if (has_target()) { 1489e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_START); 1490e5c87b76SStratos Karafotis if (!ret) 1491e5c87b76SStratos Karafotis ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 1492e5c87b76SStratos Karafotis 1493e5c87b76SStratos Karafotis if (ret) { 1494e5c87b76SStratos Karafotis pr_err("%s: Failed to start governor\n", __func__); 14953de9bdebSViresh Kumar return ret; 14963de9bdebSViresh Kumar } 1497b8eed8afSViresh Kumar } 14981da177e4SLinus Torvalds 14991da177e4SLinus Torvalds return 0; 15001da177e4SLinus Torvalds } 15011da177e4SLinus Torvalds 1502cedb70afSSrivatsa S. Bhat /** 150327a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1504cedb70afSSrivatsa S. Bhat * 1505cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1506cedb70afSSrivatsa S. Bhat */ 15078a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 15085a01f2e8SVenkatesh Pallipadi { 15098a25a2fdSKay Sievers unsigned int cpu = dev->id; 151027a862e9SViresh Kumar int ret; 1511ec28297aSVenki Pallipadi 1512ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1513ec28297aSVenki Pallipadi return 0; 1514ec28297aSVenki Pallipadi 151596bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_prepare(dev, sif); 151627a862e9SViresh Kumar 151727a862e9SViresh Kumar if (!ret) 151896bbbe4aSViresh Kumar ret = __cpufreq_remove_dev_finish(dev, sif); 151927a862e9SViresh Kumar 152027a862e9SViresh Kumar return ret; 15215a01f2e8SVenkatesh Pallipadi } 15225a01f2e8SVenkatesh Pallipadi 152365f27f38SDavid Howells static void handle_update(struct work_struct *work) 15241da177e4SLinus Torvalds { 152565f27f38SDavid Howells struct cpufreq_policy *policy = 152665f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 152765f27f38SDavid Howells unsigned int cpu = policy->cpu; 15282d06d8c4SDominik Brodowski pr_debug("handle_update for cpu %u called\n", cpu); 15291da177e4SLinus Torvalds cpufreq_update_policy(cpu); 15301da177e4SLinus Torvalds } 15311da177e4SLinus Torvalds 15321da177e4SLinus Torvalds /** 1533bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1534bb176f7dSViresh Kumar * in deep trouble. 1535a1e1dc41SViresh Kumar * @policy: policy managing CPUs 15361da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 15371da177e4SLinus Torvalds * 153829464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 153929464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 15401da177e4SLinus Torvalds */ 1541a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1542e08f5f5bSGautham R Shenoy unsigned int new_freq) 15431da177e4SLinus Torvalds { 15441da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1545b43a7ffbSViresh Kumar 1546e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1547a1e1dc41SViresh Kumar policy->cur, new_freq); 15481da177e4SLinus Torvalds 1549a1e1dc41SViresh Kumar freqs.old = policy->cur; 15501da177e4SLinus Torvalds freqs.new = new_freq; 1551b43a7ffbSViresh Kumar 15528fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 15538fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 15541da177e4SLinus Torvalds } 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds /** 15574ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 155895235ca2SVenkatesh Pallipadi * @cpu: CPU number 155995235ca2SVenkatesh Pallipadi * 156095235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 156195235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 156295235ca2SVenkatesh Pallipadi */ 156395235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 156495235ca2SVenkatesh Pallipadi { 15659e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1566e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 156795235ca2SVenkatesh Pallipadi 15681c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 15691c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 15709e21ba8bSDirk Brandewie 15719e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 157295235ca2SVenkatesh Pallipadi if (policy) { 1573e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 157495235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 157595235ca2SVenkatesh Pallipadi } 157695235ca2SVenkatesh Pallipadi 15774d34a67dSDave Jones return ret_freq; 157895235ca2SVenkatesh Pallipadi } 157995235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 158095235ca2SVenkatesh Pallipadi 15813d737108SJesse Barnes /** 15823d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 15833d737108SJesse Barnes * @cpu: CPU number 15843d737108SJesse Barnes * 15853d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 15863d737108SJesse Barnes */ 15873d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 15883d737108SJesse Barnes { 15893d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 15903d737108SJesse Barnes unsigned int ret_freq = 0; 15913d737108SJesse Barnes 15923d737108SJesse Barnes if (policy) { 15933d737108SJesse Barnes ret_freq = policy->max; 15943d737108SJesse Barnes cpufreq_cpu_put(policy); 15953d737108SJesse Barnes } 15963d737108SJesse Barnes 15973d737108SJesse Barnes return ret_freq; 15983d737108SJesse Barnes } 15993d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 16003d737108SJesse Barnes 1601d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 16021da177e4SLinus Torvalds { 1603e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 16041da177e4SLinus Torvalds 16051c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 16064d34a67dSDave Jones return ret_freq; 16071da177e4SLinus Torvalds 1608d92d50a4SViresh Kumar ret_freq = cpufreq_driver->get(policy->cpu); 16091da177e4SLinus Torvalds 1610e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 16111c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1612e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1613e08f5f5bSGautham R Shenoy saved value exists */ 1614e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1615a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, ret_freq); 16161da177e4SLinus Torvalds schedule_work(&policy->update); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 16204d34a67dSDave Jones return ret_freq; 16215a01f2e8SVenkatesh Pallipadi } 16221da177e4SLinus Torvalds 16235a01f2e8SVenkatesh Pallipadi /** 16245a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 16255a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 16265a01f2e8SVenkatesh Pallipadi * 16275a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 16285a01f2e8SVenkatesh Pallipadi */ 16295a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 16305a01f2e8SVenkatesh Pallipadi { 1631999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 16325a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 16335a01f2e8SVenkatesh Pallipadi 1634999976e0SAaron Plattner if (policy) { 1635ad7722daSviresh kumar down_read(&policy->rwsem); 1636d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1637ad7722daSviresh kumar up_read(&policy->rwsem); 1638999976e0SAaron Plattner 1639999976e0SAaron Plattner cpufreq_cpu_put(policy); 1640999976e0SAaron Plattner } 16416eed9404SViresh Kumar 16424d34a67dSDave Jones return ret_freq; 16431da177e4SLinus Torvalds } 16441da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 16451da177e4SLinus Torvalds 16468a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 16478a25a2fdSKay Sievers .name = "cpufreq", 16488a25a2fdSKay Sievers .subsys = &cpu_subsys, 16498a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 16508a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1651e00e56dfSRafael J. Wysocki }; 1652e00e56dfSRafael J. Wysocki 1653e28867eaSViresh Kumar /* 1654e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1655e28867eaSViresh Kumar * during suspend.. 165642d4dc3fSBenjamin Herrenschmidt */ 1657e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 165842d4dc3fSBenjamin Herrenschmidt { 1659e28867eaSViresh Kumar int ret; 16604bc5d341SDave Jones 1661e28867eaSViresh Kumar if (!policy->suspend_freq) { 1662e28867eaSViresh Kumar pr_err("%s: suspend_freq can't be zero\n", __func__); 1663e28867eaSViresh Kumar return -EINVAL; 166442d4dc3fSBenjamin Herrenschmidt } 166542d4dc3fSBenjamin Herrenschmidt 1666e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1667e28867eaSViresh Kumar policy->suspend_freq); 1668e28867eaSViresh Kumar 1669e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1670e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1671e28867eaSViresh Kumar if (ret) 1672e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1673e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1674e28867eaSViresh Kumar 1675c9060494SDave Jones return ret; 167642d4dc3fSBenjamin Herrenschmidt } 1677e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 167842d4dc3fSBenjamin Herrenschmidt 167942d4dc3fSBenjamin Herrenschmidt /** 16802f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 16811da177e4SLinus Torvalds * 16822f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 16832f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 16842f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 16852f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 16861da177e4SLinus Torvalds */ 16872f0aea93SViresh Kumar void cpufreq_suspend(void) 16881da177e4SLinus Torvalds { 16893a3e9e06SViresh Kumar struct cpufreq_policy *policy; 16901da177e4SLinus Torvalds 16912f0aea93SViresh Kumar if (!cpufreq_driver) 1692e00e56dfSRafael J. Wysocki return; 16931da177e4SLinus Torvalds 16942f0aea93SViresh Kumar if (!has_target()) 1695b1b12babSViresh Kumar goto suspend; 16961da177e4SLinus Torvalds 16972f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 16982f0aea93SViresh Kumar 1699f963735aSViresh Kumar for_each_active_policy(policy) { 17002f0aea93SViresh Kumar if (__cpufreq_governor(policy, CPUFREQ_GOV_STOP)) 17012f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 17022f0aea93SViresh Kumar __func__, policy); 17032f0aea93SViresh Kumar else if (cpufreq_driver->suspend 17042f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 17052f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 17062f0aea93SViresh Kumar policy); 17071da177e4SLinus Torvalds } 1708b1b12babSViresh Kumar 1709b1b12babSViresh Kumar suspend: 1710b1b12babSViresh Kumar cpufreq_suspended = true; 17111da177e4SLinus Torvalds } 17121da177e4SLinus Torvalds 17131da177e4SLinus Torvalds /** 17142f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 17151da177e4SLinus Torvalds * 17162f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 17172f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 17181da177e4SLinus Torvalds */ 17192f0aea93SViresh Kumar void cpufreq_resume(void) 17201da177e4SLinus Torvalds { 17211da177e4SLinus Torvalds struct cpufreq_policy *policy; 17221da177e4SLinus Torvalds 17232f0aea93SViresh Kumar if (!cpufreq_driver) 17241da177e4SLinus Torvalds return; 17251da177e4SLinus Torvalds 17268e30444eSLan Tianyu cpufreq_suspended = false; 17278e30444eSLan Tianyu 17282f0aea93SViresh Kumar if (!has_target()) 17292f0aea93SViresh Kumar return; 17301da177e4SLinus Torvalds 17312f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 17322f0aea93SViresh Kumar 1733f963735aSViresh Kumar for_each_active_policy(policy) { 17340c5aa405SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) 17350c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 17360c5aa405SViresh Kumar policy); 17370c5aa405SViresh Kumar else if (__cpufreq_governor(policy, CPUFREQ_GOV_START) 17382f0aea93SViresh Kumar || __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS)) 17392f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 17402f0aea93SViresh Kumar __func__, policy); 1741c75de0acSViresh Kumar } 17422f0aea93SViresh Kumar 17432f0aea93SViresh Kumar /* 1744c75de0acSViresh Kumar * schedule call cpufreq_update_policy() for first-online CPU, as that 1745c75de0acSViresh Kumar * wouldn't be hotplugged-out on suspend. It will verify that the 1746c75de0acSViresh Kumar * current freq is in sync with what we believe it to be. 17472f0aea93SViresh Kumar */ 1748c75de0acSViresh Kumar policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask)); 1749c75de0acSViresh Kumar if (WARN_ON(!policy)) 1750c75de0acSViresh Kumar return; 1751c75de0acSViresh Kumar 17523a3e9e06SViresh Kumar schedule_work(&policy->update); 17531da177e4SLinus Torvalds } 17541da177e4SLinus Torvalds 17559d95046eSBorislav Petkov /** 17569d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 17579d95046eSBorislav Petkov * 17589d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 17599d95046eSBorislav Petkov * or NULL, if none. 17609d95046eSBorislav Petkov */ 17619d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 17629d95046eSBorislav Petkov { 17631c3d85ddSRafael J. Wysocki if (cpufreq_driver) 17641c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 17651c3d85ddSRafael J. Wysocki 17661c3d85ddSRafael J. Wysocki return NULL; 17679d95046eSBorislav Petkov } 17689d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 17691da177e4SLinus Torvalds 177051315cdfSThomas Petazzoni /** 177151315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 177251315cdfSThomas Petazzoni * 177351315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 177451315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 177551315cdfSThomas Petazzoni */ 177651315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 177751315cdfSThomas Petazzoni { 177851315cdfSThomas Petazzoni if (cpufreq_driver) 177951315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 178051315cdfSThomas Petazzoni 178151315cdfSThomas Petazzoni return NULL; 178251315cdfSThomas Petazzoni } 178351315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 178451315cdfSThomas Petazzoni 17851da177e4SLinus Torvalds /********************************************************************* 17861da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 17871da177e4SLinus Torvalds *********************************************************************/ 17881da177e4SLinus Torvalds 17891da177e4SLinus Torvalds /** 17901da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 17911da177e4SLinus Torvalds * @nb: notifier function to register 17921da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 17931da177e4SLinus Torvalds * 17941da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 17951da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 17961da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 17971da177e4SLinus Torvalds * changes in cpufreq policy. 17981da177e4SLinus Torvalds * 17991da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1800e041c683SAlan Stern * blocking_notifier_chain_register. 18011da177e4SLinus Torvalds */ 18021da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 18031da177e4SLinus Torvalds { 18041da177e4SLinus Torvalds int ret; 18051da177e4SLinus Torvalds 1806d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1807d5aaffa9SDirk Brandewie return -EINVAL; 1808d5aaffa9SDirk Brandewie 180974212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 181074212ca4SCesar Eduardo Barros 18111da177e4SLinus Torvalds switch (list) { 18121da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1813b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1814e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 18151da177e4SLinus Torvalds break; 18161da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1817e041c683SAlan Stern ret = blocking_notifier_chain_register( 1818e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18191da177e4SLinus Torvalds break; 18201da177e4SLinus Torvalds default: 18211da177e4SLinus Torvalds ret = -EINVAL; 18221da177e4SLinus Torvalds } 18231da177e4SLinus Torvalds 18241da177e4SLinus Torvalds return ret; 18251da177e4SLinus Torvalds } 18261da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 18271da177e4SLinus Torvalds 18281da177e4SLinus Torvalds /** 18291da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 18301da177e4SLinus Torvalds * @nb: notifier block to be unregistered 18311da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 18321da177e4SLinus Torvalds * 18331da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 18341da177e4SLinus Torvalds * 18351da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1836e041c683SAlan Stern * blocking_notifier_chain_unregister. 18371da177e4SLinus Torvalds */ 18381da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 18391da177e4SLinus Torvalds { 18401da177e4SLinus Torvalds int ret; 18411da177e4SLinus Torvalds 1842d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1843d5aaffa9SDirk Brandewie return -EINVAL; 1844d5aaffa9SDirk Brandewie 18451da177e4SLinus Torvalds switch (list) { 18461da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1847b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1848e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 18491da177e4SLinus Torvalds break; 18501da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1851e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1852e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 18531da177e4SLinus Torvalds break; 18541da177e4SLinus Torvalds default: 18551da177e4SLinus Torvalds ret = -EINVAL; 18561da177e4SLinus Torvalds } 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds return ret; 18591da177e4SLinus Torvalds } 18601da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 18611da177e4SLinus Torvalds 18621da177e4SLinus Torvalds 18631da177e4SLinus Torvalds /********************************************************************* 18641da177e4SLinus Torvalds * GOVERNORS * 18651da177e4SLinus Torvalds *********************************************************************/ 18661da177e4SLinus Torvalds 18671c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 18681c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 18691c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 18701c03a2d0SViresh Kumar { 18711c03a2d0SViresh Kumar int ret; 18721c03a2d0SViresh Kumar 18731c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 18741c03a2d0SViresh Kumar 18751c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 18761c03a2d0SViresh Kumar if (!freqs->new) 18771c03a2d0SViresh Kumar return 0; 18781c03a2d0SViresh Kumar 18791c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 18801c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 18811c03a2d0SViresh Kumar 18821c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 18831c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 18841c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 18851c03a2d0SViresh Kumar 18861c03a2d0SViresh Kumar if (ret) 18871c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 18881c03a2d0SViresh Kumar __func__, ret); 18891c03a2d0SViresh Kumar 18901c03a2d0SViresh Kumar return ret; 18911c03a2d0SViresh Kumar } 18921c03a2d0SViresh Kumar 18938d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy, 18948d65775dSViresh Kumar struct cpufreq_frequency_table *freq_table, int index) 18958d65775dSViresh Kumar { 18961c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 18971c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 18988d65775dSViresh Kumar int retval = -EINVAL; 18998d65775dSViresh Kumar bool notify; 19008d65775dSViresh Kumar 19018d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 19028d65775dSViresh Kumar if (notify) { 19031c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 19041c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 19051c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 19061c03a2d0SViresh Kumar if (retval) 19071c03a2d0SViresh Kumar return retval; 19088d65775dSViresh Kumar 19091c03a2d0SViresh Kumar intermediate_freq = freqs.new; 19101c03a2d0SViresh Kumar /* Set old freq to intermediate */ 19111c03a2d0SViresh Kumar if (intermediate_freq) 19121c03a2d0SViresh Kumar freqs.old = freqs.new; 19131c03a2d0SViresh Kumar } 19141c03a2d0SViresh Kumar 19151c03a2d0SViresh Kumar freqs.new = freq_table[index].frequency; 19168d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 19178d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 19188d65775dSViresh Kumar 19198d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 19208d65775dSViresh Kumar } 19218d65775dSViresh Kumar 19228d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 19238d65775dSViresh Kumar if (retval) 19248d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 19258d65775dSViresh Kumar retval); 19268d65775dSViresh Kumar 19271c03a2d0SViresh Kumar if (notify) { 19288d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 19298d65775dSViresh Kumar 19301c03a2d0SViresh Kumar /* 19311c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 19321c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 19331c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 1934*58405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 19351c03a2d0SViresh Kumar */ 19361c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 19371c03a2d0SViresh Kumar freqs.old = intermediate_freq; 19381c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 19391c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 19401c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 19411c03a2d0SViresh Kumar } 19421c03a2d0SViresh Kumar } 19431c03a2d0SViresh Kumar 19448d65775dSViresh Kumar return retval; 19458d65775dSViresh Kumar } 19468d65775dSViresh Kumar 19471da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 19481da177e4SLinus Torvalds unsigned int target_freq, 19491da177e4SLinus Torvalds unsigned int relation) 19501da177e4SLinus Torvalds { 19517249924eSViresh Kumar unsigned int old_target_freq = target_freq; 19528d65775dSViresh Kumar int retval = -EINVAL; 1953c32b6b8eSAshok Raj 1954a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1955a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1956a7b422cdSKonrad Rzeszutek Wilk 19577249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 19587249924eSViresh Kumar if (target_freq > policy->max) 19597249924eSViresh Kumar target_freq = policy->max; 19607249924eSViresh Kumar if (target_freq < policy->min) 19617249924eSViresh Kumar target_freq = policy->min; 19627249924eSViresh Kumar 19637249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 19647249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 19655a1c0228SViresh Kumar 19669c0ebcf7SViresh Kumar /* 19679c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 19689c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 19699c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 19709c0ebcf7SViresh Kumar * calls. 19719c0ebcf7SViresh Kumar */ 19725a1c0228SViresh Kumar if (target_freq == policy->cur) 19735a1c0228SViresh Kumar return 0; 19745a1c0228SViresh Kumar 19751c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 19761c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 19771c03a2d0SViresh Kumar 19781c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 19791c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 19809c0ebcf7SViresh Kumar else if (cpufreq_driver->target_index) { 19819c0ebcf7SViresh Kumar struct cpufreq_frequency_table *freq_table; 19829c0ebcf7SViresh Kumar int index; 198390d45d17SAshok Raj 19849c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 19859c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 19869c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 19879c0ebcf7SViresh Kumar goto out; 19889c0ebcf7SViresh Kumar } 19899c0ebcf7SViresh Kumar 19909c0ebcf7SViresh Kumar retval = cpufreq_frequency_table_target(policy, freq_table, 19919c0ebcf7SViresh Kumar target_freq, relation, &index); 19929c0ebcf7SViresh Kumar if (unlikely(retval)) { 19939c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 19949c0ebcf7SViresh Kumar goto out; 19959c0ebcf7SViresh Kumar } 19969c0ebcf7SViresh Kumar 1997d4019f0aSViresh Kumar if (freq_table[index].frequency == policy->cur) { 19989c0ebcf7SViresh Kumar retval = 0; 1999d4019f0aSViresh Kumar goto out; 2000d4019f0aSViresh Kumar } 2001d4019f0aSViresh Kumar 20028d65775dSViresh Kumar retval = __target_index(policy, freq_table, index); 20039c0ebcf7SViresh Kumar } 20049c0ebcf7SViresh Kumar 20059c0ebcf7SViresh Kumar out: 20061da177e4SLinus Torvalds return retval; 20071da177e4SLinus Torvalds } 20081da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 20091da177e4SLinus Torvalds 20101da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 20111da177e4SLinus Torvalds unsigned int target_freq, 20121da177e4SLinus Torvalds unsigned int relation) 20131da177e4SLinus Torvalds { 2014f1829e4aSJulia Lawall int ret = -EINVAL; 20151da177e4SLinus Torvalds 2016ad7722daSviresh kumar down_write(&policy->rwsem); 20171da177e4SLinus Torvalds 20181da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 20191da177e4SLinus Torvalds 2020ad7722daSviresh kumar up_write(&policy->rwsem); 20211da177e4SLinus Torvalds 20221da177e4SLinus Torvalds return ret; 20231da177e4SLinus Torvalds } 20241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 20251da177e4SLinus Torvalds 2026e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 2027e08f5f5bSGautham R Shenoy unsigned int event) 20281da177e4SLinus Torvalds { 2029cc993cabSDave Jones int ret; 20306afde10cSThomas Renninger 20316afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 20326afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 20336afde10cSThomas Renninger That this is the case is already ensured in Kconfig 20346afde10cSThomas Renninger */ 20356afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 20366afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 20376afde10cSThomas Renninger #else 20386afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 20396afde10cSThomas Renninger #endif 20401c256245SThomas Renninger 20412f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 20422f0aea93SViresh Kumar if (cpufreq_suspended) 20432f0aea93SViresh Kumar return 0; 2044cb57720bSEthan Zhao /* 2045cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 2046cb57720bSEthan Zhao * notification happened, so check it. 2047cb57720bSEthan Zhao */ 2048cb57720bSEthan Zhao if (!policy->governor) 2049cb57720bSEthan Zhao return -EINVAL; 20502f0aea93SViresh Kumar 20511c256245SThomas Renninger if (policy->governor->max_transition_latency && 20521c256245SThomas Renninger policy->cpuinfo.transition_latency > 20531c256245SThomas Renninger policy->governor->max_transition_latency) { 20546afde10cSThomas Renninger if (!gov) 20556afde10cSThomas Renninger return -EINVAL; 20566afde10cSThomas Renninger else { 2057e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 2058e837f9b5SJoe Perches policy->governor->name, gov->name); 20591c256245SThomas Renninger policy->governor = gov; 20601c256245SThomas Renninger } 20616afde10cSThomas Renninger } 20621da177e4SLinus Torvalds 2063fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20641da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 20651da177e4SLinus Torvalds return -EINVAL; 20661da177e4SLinus Torvalds 20672d06d8c4SDominik Brodowski pr_debug("__cpufreq_governor for CPU %u, event %u\n", 2068e08f5f5bSGautham R Shenoy policy->cpu, event); 206995731ebbSXiaoguang Chen 207095731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 207156d07db2SSrivatsa S. Bhat if ((policy->governor_enabled && event == CPUFREQ_GOV_START) 2072f73d3933SViresh Kumar || (!policy->governor_enabled 2073f73d3933SViresh Kumar && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) { 207495731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 207595731ebbSXiaoguang Chen return -EBUSY; 207695731ebbSXiaoguang Chen } 207795731ebbSXiaoguang Chen 207895731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 207995731ebbSXiaoguang Chen policy->governor_enabled = false; 208095731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 208195731ebbSXiaoguang Chen policy->governor_enabled = true; 208295731ebbSXiaoguang Chen 208395731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 208495731ebbSXiaoguang Chen 20851da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 20861da177e4SLinus Torvalds 20874d5dcc42SViresh Kumar if (!ret) { 20884d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 20898e53695fSViresh Kumar policy->governor->initialized++; 20904d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 20918e53695fSViresh Kumar policy->governor->initialized--; 209295731ebbSXiaoguang Chen } else { 209395731ebbSXiaoguang Chen /* Restore original values */ 209495731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 209595731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 209695731ebbSXiaoguang Chen policy->governor_enabled = true; 209795731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 209895731ebbSXiaoguang Chen policy->governor_enabled = false; 209995731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 21004d5dcc42SViresh Kumar } 2101b394058fSViresh Kumar 2102fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 2103fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 21041da177e4SLinus Torvalds module_put(policy->governor->owner); 21051da177e4SLinus Torvalds 21061da177e4SLinus Torvalds return ret; 21071da177e4SLinus Torvalds } 21081da177e4SLinus Torvalds 21091da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 21101da177e4SLinus Torvalds { 21113bcb09a3SJeremy Fitzhardinge int err; 21121da177e4SLinus Torvalds 21131da177e4SLinus Torvalds if (!governor) 21141da177e4SLinus Torvalds return -EINVAL; 21151da177e4SLinus Torvalds 2116a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2117a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2118a7b422cdSKonrad Rzeszutek Wilk 21193fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 21201da177e4SLinus Torvalds 2121b394058fSViresh Kumar governor->initialized = 0; 21223bcb09a3SJeremy Fitzhardinge err = -EBUSY; 212342f91fa1SViresh Kumar if (!find_governor(governor->name)) { 21243bcb09a3SJeremy Fitzhardinge err = 0; 21251da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 21263bcb09a3SJeremy Fitzhardinge } 21271da177e4SLinus Torvalds 21283fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 21293bcb09a3SJeremy Fitzhardinge return err; 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 21321da177e4SLinus Torvalds 21331da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 21341da177e4SLinus Torvalds { 21354573237bSViresh Kumar struct cpufreq_policy *policy; 21364573237bSViresh Kumar unsigned long flags; 213790e41bacSPrarit Bhargava 21381da177e4SLinus Torvalds if (!governor) 21391da177e4SLinus Torvalds return; 21401da177e4SLinus Torvalds 2141a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2142a7b422cdSKonrad Rzeszutek Wilk return; 2143a7b422cdSKonrad Rzeszutek Wilk 21444573237bSViresh Kumar /* clear last_governor for all inactive policies */ 21454573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 21464573237bSViresh Kumar for_each_inactive_policy(policy) { 214718bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 214818bf3a12SViresh Kumar policy->governor = NULL; 21494573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 215090e41bacSPrarit Bhargava } 215118bf3a12SViresh Kumar } 21524573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 215390e41bacSPrarit Bhargava 21543fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 21551da177e4SLinus Torvalds list_del(&governor->governor_list); 21563fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 21571da177e4SLinus Torvalds return; 21581da177e4SLinus Torvalds } 21591da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 21601da177e4SLinus Torvalds 21611da177e4SLinus Torvalds 21621da177e4SLinus Torvalds /********************************************************************* 21631da177e4SLinus Torvalds * POLICY INTERFACE * 21641da177e4SLinus Torvalds *********************************************************************/ 21651da177e4SLinus Torvalds 21661da177e4SLinus Torvalds /** 21671da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 216829464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 216929464f28SDave Jones * is written 21701da177e4SLinus Torvalds * 21711da177e4SLinus Torvalds * Reads the current cpufreq policy. 21721da177e4SLinus Torvalds */ 21731da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 21741da177e4SLinus Torvalds { 21751da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 21761da177e4SLinus Torvalds if (!policy) 21771da177e4SLinus Torvalds return -EINVAL; 21781da177e4SLinus Torvalds 21791da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 21801da177e4SLinus Torvalds if (!cpu_policy) 21811da177e4SLinus Torvalds return -EINVAL; 21821da177e4SLinus Torvalds 2183d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 21841da177e4SLinus Torvalds 21851da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 21861da177e4SLinus Torvalds return 0; 21871da177e4SLinus Torvalds } 21881da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 21891da177e4SLinus Torvalds 2190153d7f3fSArjan van de Ven /* 2191037ce839SViresh Kumar * policy : current policy. 2192037ce839SViresh Kumar * new_policy: policy to be set. 2193153d7f3fSArjan van de Ven */ 2194037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 21953a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 21961da177e4SLinus Torvalds { 2197d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2198d9a789c7SRafael J. Wysocki int ret; 21991da177e4SLinus Torvalds 2200e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2201e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 22021da177e4SLinus Torvalds 2203d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 22041da177e4SLinus Torvalds 2205d9a789c7SRafael J. Wysocki if (new_policy->min > policy->max || new_policy->max < policy->min) 2206d9a789c7SRafael J. Wysocki return -EINVAL; 22079c9a43edSMattia Dongili 22081da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 22093a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 22101da177e4SLinus Torvalds if (ret) 2211d9a789c7SRafael J. Wysocki return ret; 22121da177e4SLinus Torvalds 22131da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2214e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 22153a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 22161da177e4SLinus Torvalds 22171da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 2218e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 22193a3e9e06SViresh Kumar CPUFREQ_INCOMPATIBLE, new_policy); 22201da177e4SLinus Torvalds 2221bb176f7dSViresh Kumar /* 2222bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2223bb176f7dSViresh Kumar * different to the first one 2224bb176f7dSViresh Kumar */ 22253a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2226e041c683SAlan Stern if (ret) 2227d9a789c7SRafael J. Wysocki return ret; 22281da177e4SLinus Torvalds 22291da177e4SLinus Torvalds /* notification of the new policy */ 2230e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 22313a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 22321da177e4SLinus Torvalds 22333a3e9e06SViresh Kumar policy->min = new_policy->min; 22343a3e9e06SViresh Kumar policy->max = new_policy->max; 22351da177e4SLinus Torvalds 22362d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 22373a3e9e06SViresh Kumar policy->min, policy->max); 22381da177e4SLinus Torvalds 22391c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 22403a3e9e06SViresh Kumar policy->policy = new_policy->policy; 22412d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2242d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2243d9a789c7SRafael J. Wysocki } 2244d9a789c7SRafael J. Wysocki 2245d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2246d9a789c7SRafael J. Wysocki goto out; 22471da177e4SLinus Torvalds 22482d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 22491da177e4SLinus Torvalds 2250d9a789c7SRafael J. Wysocki /* save old, working values */ 2251d9a789c7SRafael J. Wysocki old_gov = policy->governor; 22521da177e4SLinus Torvalds /* end old governor */ 2253d9a789c7SRafael J. Wysocki if (old_gov) { 22543a3e9e06SViresh Kumar __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 2255ad7722daSviresh kumar up_write(&policy->rwsem); 2256d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2257ad7722daSviresh kumar down_write(&policy->rwsem); 22587bd353a9SViresh Kumar } 22591da177e4SLinus Torvalds 22601da177e4SLinus Torvalds /* start new governor */ 22613a3e9e06SViresh Kumar policy->governor = new_policy->governor; 22623a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) { 2263d9a789c7SRafael J. Wysocki if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) 2264d9a789c7SRafael J. Wysocki goto out; 2265d9a789c7SRafael J. Wysocki 2266ad7722daSviresh kumar up_write(&policy->rwsem); 2267d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2268ad7722daSviresh kumar down_write(&policy->rwsem); 2269955ef483SViresh Kumar } 22707bd353a9SViresh Kumar 22711da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2272d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 22731da177e4SLinus Torvalds if (old_gov) { 22743a3e9e06SViresh Kumar policy->governor = old_gov; 2275d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 2276d9a789c7SRafael J. Wysocki __cpufreq_governor(policy, CPUFREQ_GOV_START); 22771da177e4SLinus Torvalds } 22781da177e4SLinus Torvalds 2279d9a789c7SRafael J. Wysocki return -EINVAL; 2280d9a789c7SRafael J. Wysocki 2281d9a789c7SRafael J. Wysocki out: 2282d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2283d9a789c7SRafael J. Wysocki return __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 22841da177e4SLinus Torvalds } 22851da177e4SLinus Torvalds 22861da177e4SLinus Torvalds /** 22871da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 22881da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 22891da177e4SLinus Torvalds * 229025985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 22911da177e4SLinus Torvalds * at different times. 22921da177e4SLinus Torvalds */ 22931da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 22941da177e4SLinus Torvalds { 22953a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 22963a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2297f1829e4aSJulia Lawall int ret; 22981da177e4SLinus Torvalds 2299fefa8ff8SAaron Plattner if (!policy) 2300fefa8ff8SAaron Plattner return -ENODEV; 23011da177e4SLinus Torvalds 2302ad7722daSviresh kumar down_write(&policy->rwsem); 23031da177e4SLinus Torvalds 23042d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2305d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 23063a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 23073a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 23083a3e9e06SViresh Kumar new_policy.policy = policy->user_policy.policy; 23093a3e9e06SViresh Kumar new_policy.governor = policy->user_policy.governor; 23101da177e4SLinus Torvalds 2311bb176f7dSViresh Kumar /* 2312bb176f7dSViresh Kumar * BIOS might change freq behind our back 2313bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2314bb176f7dSViresh Kumar */ 23152ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 23163a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2317bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2318bd0fa9bbSViresh Kumar ret = -EIO; 2319fefa8ff8SAaron Plattner goto unlock; 2320bd0fa9bbSViresh Kumar } 2321bd0fa9bbSViresh Kumar 23223a3e9e06SViresh Kumar if (!policy->cur) { 2323e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 23243a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2325a85f7bd3SThomas Renninger } else { 23269c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 2327a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, new_policy.cur); 23280961dd0dSThomas Renninger } 2329a85f7bd3SThomas Renninger } 23300961dd0dSThomas Renninger 2331037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 23321da177e4SLinus Torvalds 2333fefa8ff8SAaron Plattner unlock: 2334ad7722daSviresh kumar up_write(&policy->rwsem); 23355a01f2e8SVenkatesh Pallipadi 23363a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 23371da177e4SLinus Torvalds return ret; 23381da177e4SLinus Torvalds } 23391da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 23401da177e4SLinus Torvalds 23412760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2342c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2343c32b6b8eSAshok Raj { 2344c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 23458a25a2fdSKay Sievers struct device *dev; 2346c32b6b8eSAshok Raj 23478a25a2fdSKay Sievers dev = get_cpu_device(cpu); 23488a25a2fdSKay Sievers if (dev) { 23495302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2350c32b6b8eSAshok Raj case CPU_ONLINE: 235123faf0b7SViresh Kumar cpufreq_add_dev(dev, NULL); 2352c32b6b8eSAshok Raj break; 23535302c3fbSSrivatsa S. Bhat 2354c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 235596bbbe4aSViresh Kumar __cpufreq_remove_dev_prepare(dev, NULL); 23561aee40acSSrivatsa S. Bhat break; 23571aee40acSSrivatsa S. Bhat 23581aee40acSSrivatsa S. Bhat case CPU_POST_DEAD: 235996bbbe4aSViresh Kumar __cpufreq_remove_dev_finish(dev, NULL); 2360c32b6b8eSAshok Raj break; 23615302c3fbSSrivatsa S. Bhat 23625a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 236323faf0b7SViresh Kumar cpufreq_add_dev(dev, NULL); 2364c32b6b8eSAshok Raj break; 2365c32b6b8eSAshok Raj } 2366c32b6b8eSAshok Raj } 2367c32b6b8eSAshok Raj return NOTIFY_OK; 2368c32b6b8eSAshok Raj } 2369c32b6b8eSAshok Raj 23709c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2371c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2372c32b6b8eSAshok Raj }; 23731da177e4SLinus Torvalds 23741da177e4SLinus Torvalds /********************************************************************* 23756f19efc0SLukasz Majewski * BOOST * 23766f19efc0SLukasz Majewski *********************************************************************/ 23776f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 23786f19efc0SLukasz Majewski { 23796f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 23806f19efc0SLukasz Majewski struct cpufreq_policy *policy; 23816f19efc0SLukasz Majewski int ret = -EINVAL; 23826f19efc0SLukasz Majewski 2383f963735aSViresh Kumar for_each_active_policy(policy) { 23846f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 23856f19efc0SLukasz Majewski if (freq_table) { 23866f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 23876f19efc0SLukasz Majewski freq_table); 23886f19efc0SLukasz Majewski if (ret) { 23896f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 23906f19efc0SLukasz Majewski __func__); 23916f19efc0SLukasz Majewski break; 23926f19efc0SLukasz Majewski } 23936f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 23946f19efc0SLukasz Majewski __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 23956f19efc0SLukasz Majewski } 23966f19efc0SLukasz Majewski } 23976f19efc0SLukasz Majewski 23986f19efc0SLukasz Majewski return ret; 23996f19efc0SLukasz Majewski } 24006f19efc0SLukasz Majewski 24016f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 24026f19efc0SLukasz Majewski { 24036f19efc0SLukasz Majewski unsigned long flags; 24046f19efc0SLukasz Majewski int ret = 0; 24056f19efc0SLukasz Majewski 24066f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 24076f19efc0SLukasz Majewski return 0; 24086f19efc0SLukasz Majewski 24096f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24106f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 24116f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24126f19efc0SLukasz Majewski 24136f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 24146f19efc0SLukasz Majewski if (ret) { 24156f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 24166f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 24176f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24186f19efc0SLukasz Majewski 2419e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2420e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 24216f19efc0SLukasz Majewski } 24226f19efc0SLukasz Majewski 24236f19efc0SLukasz Majewski return ret; 24246f19efc0SLukasz Majewski } 24256f19efc0SLukasz Majewski 24266f19efc0SLukasz Majewski int cpufreq_boost_supported(void) 24276f19efc0SLukasz Majewski { 24286f19efc0SLukasz Majewski if (likely(cpufreq_driver)) 24296f19efc0SLukasz Majewski return cpufreq_driver->boost_supported; 24306f19efc0SLukasz Majewski 24316f19efc0SLukasz Majewski return 0; 24326f19efc0SLukasz Majewski } 24336f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_supported); 24346f19efc0SLukasz Majewski 24356f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 24366f19efc0SLukasz Majewski { 24376f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 24386f19efc0SLukasz Majewski } 24396f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 24406f19efc0SLukasz Majewski 24416f19efc0SLukasz Majewski /********************************************************************* 24421da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 24431da177e4SLinus Torvalds *********************************************************************/ 24441da177e4SLinus Torvalds 24451da177e4SLinus Torvalds /** 24461da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 24471da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 24481da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 24491da177e4SLinus Torvalds * 24501da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 24511da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 24521da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 24531da177e4SLinus Torvalds * 24541da177e4SLinus Torvalds */ 2455221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 24561da177e4SLinus Torvalds { 24571da177e4SLinus Torvalds unsigned long flags; 24581da177e4SLinus Torvalds int ret; 24591da177e4SLinus Torvalds 2460a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2461a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2462a7b422cdSKonrad Rzeszutek Wilk 24631da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 24649c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 24659832235fSRafael J. Wysocki driver_data->target) || 24669832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 24671c03a2d0SViresh Kumar driver_data->target)) || 24681c03a2d0SViresh Kumar (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) 24691da177e4SLinus Torvalds return -EINVAL; 24701da177e4SLinus Torvalds 24712d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 24721da177e4SLinus Torvalds 24730d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24741c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 24750d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24764dea5806SYinghai Lu return -EEXIST; 24771da177e4SLinus Torvalds } 24781c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 24790d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 24801da177e4SLinus Torvalds 2481bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2482bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2483bc68b7dfSViresh Kumar 24846f19efc0SLukasz Majewski if (cpufreq_boost_supported()) { 24856f19efc0SLukasz Majewski /* 24866f19efc0SLukasz Majewski * Check if driver provides function to enable boost - 24876f19efc0SLukasz Majewski * if not, use cpufreq_boost_set_sw as default 24886f19efc0SLukasz Majewski */ 24896f19efc0SLukasz Majewski if (!cpufreq_driver->set_boost) 24906f19efc0SLukasz Majewski cpufreq_driver->set_boost = cpufreq_boost_set_sw; 24916f19efc0SLukasz Majewski 24926f19efc0SLukasz Majewski ret = cpufreq_sysfs_create_file(&boost.attr); 24936f19efc0SLukasz Majewski if (ret) { 24946f19efc0SLukasz Majewski pr_err("%s: cannot register global BOOST sysfs file\n", 24956f19efc0SLukasz Majewski __func__); 24966f19efc0SLukasz Majewski goto err_null_driver; 24976f19efc0SLukasz Majewski } 24986f19efc0SLukasz Majewski } 24996f19efc0SLukasz Majewski 25008a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 25018f5bc2abSJiri Slaby if (ret) 25026f19efc0SLukasz Majewski goto err_boost_unreg; 25031da177e4SLinus Torvalds 2504ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2505ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 25061da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 2507ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2508e08f5f5bSGautham R Shenoy driver_data->name); 25098a25a2fdSKay Sievers goto err_if_unreg; 25101da177e4SLinus Torvalds } 25111da177e4SLinus Torvalds 251265edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 25132d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 25141da177e4SLinus Torvalds 25158f5bc2abSJiri Slaby return 0; 25168a25a2fdSKay Sievers err_if_unreg: 25178a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 25186f19efc0SLukasz Majewski err_boost_unreg: 25196f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 25206f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 25218f5bc2abSJiri Slaby err_null_driver: 25220d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25231c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 25240d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 25254d34a67dSDave Jones return ret; 25261da177e4SLinus Torvalds } 25271da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 25281da177e4SLinus Torvalds 25291da177e4SLinus Torvalds /** 25301da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 25311da177e4SLinus Torvalds * 25321da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 25331da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 25341da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 25351da177e4SLinus Torvalds * currently not initialised. 25361da177e4SLinus Torvalds */ 2537221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 25381da177e4SLinus Torvalds { 25391da177e4SLinus Torvalds unsigned long flags; 25401da177e4SLinus Torvalds 25411c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 25421da177e4SLinus Torvalds return -EINVAL; 25431da177e4SLinus Torvalds 25442d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 25451da177e4SLinus Torvalds 25468a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 25476f19efc0SLukasz Majewski if (cpufreq_boost_supported()) 25486f19efc0SLukasz Majewski cpufreq_sysfs_remove_file(&boost.attr); 25496f19efc0SLukasz Majewski 255065edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 25511da177e4SLinus Torvalds 25526eed9404SViresh Kumar down_write(&cpufreq_rwsem); 25530d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 25546eed9404SViresh Kumar 25551c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 25566eed9404SViresh Kumar 25570d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 25586eed9404SViresh Kumar up_write(&cpufreq_rwsem); 25591da177e4SLinus Torvalds 25601da177e4SLinus Torvalds return 0; 25611da177e4SLinus Torvalds } 25621da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 25635a01f2e8SVenkatesh Pallipadi 256490de2a4aSDoug Anderson /* 256590de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 256690de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 256790de2a4aSDoug Anderson */ 256890de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 256990de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 257090de2a4aSDoug Anderson }; 257190de2a4aSDoug Anderson 25725a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 25735a01f2e8SVenkatesh Pallipadi { 2574a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2575a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2576a7b422cdSKonrad Rzeszutek Wilk 25772361be23SViresh Kumar cpufreq_global_kobject = kobject_create(); 25788aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 25798aa84ad8SThomas Renninger 258090de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 258190de2a4aSDoug Anderson 25825a01f2e8SVenkatesh Pallipadi return 0; 25835a01f2e8SVenkatesh Pallipadi } 25845a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2585