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 /* Macros to iterate over CPU policies */ 42f963735aSViresh Kumar #define for_each_suitable_policy(__policy, __active) \ 43fd7dc7e6SEric Biggers list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \ 44fd7dc7e6SEric Biggers if ((__active) == !policy_is_inactive(__policy)) 45f963735aSViresh Kumar 46f963735aSViresh Kumar #define for_each_active_policy(__policy) \ 47f963735aSViresh Kumar for_each_suitable_policy(__policy, true) 48f963735aSViresh Kumar #define for_each_inactive_policy(__policy) \ 49f963735aSViresh Kumar for_each_suitable_policy(__policy, false) 50f963735aSViresh Kumar 51b4f0676fSViresh Kumar #define for_each_policy(__policy) \ 52b4f0676fSViresh Kumar list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) 53b4f0676fSViresh Kumar 54f7b27061SViresh Kumar /* Iterate over governors */ 55f7b27061SViresh Kumar static LIST_HEAD(cpufreq_governor_list); 56f7b27061SViresh Kumar #define for_each_governor(__governor) \ 57f7b27061SViresh Kumar list_for_each_entry(__governor, &cpufreq_governor_list, governor_list) 58f7b27061SViresh Kumar 591da177e4SLinus Torvalds /** 60cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 611da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 621da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 631da177e4SLinus Torvalds */ 641c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 657a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 66bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 67bb176f7dSViresh Kumar 682f0aea93SViresh Kumar /* Flag to suspend/resume CPUFreq governors */ 692f0aea93SViresh Kumar static bool cpufreq_suspended; 701da177e4SLinus Torvalds 719c0ebcf7SViresh Kumar static inline bool has_target(void) 729c0ebcf7SViresh Kumar { 739c0ebcf7SViresh Kumar return cpufreq_driver->target_index || cpufreq_driver->target; 749c0ebcf7SViresh Kumar } 759c0ebcf7SViresh Kumar 761da177e4SLinus Torvalds /* internal prototypes */ 77a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event); 78d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy); 791da177e4SLinus Torvalds 801da177e4SLinus Torvalds /** 811da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 821da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 831da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 841da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 851da177e4SLinus Torvalds * The mutex locks both lists. 861da177e4SLinus Torvalds */ 87e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 88b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 891da177e4SLinus Torvalds 9074212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 91b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 92b4dfdbb3SAlan Stern { 93b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 9474212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 95b4dfdbb3SAlan Stern return 0; 96b4dfdbb3SAlan Stern } 97b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 981da177e4SLinus Torvalds 99a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 100da584455SViresh Kumar static int cpufreq_disabled(void) 101a7b422cdSKonrad Rzeszutek Wilk { 102a7b422cdSKonrad Rzeszutek Wilk return off; 103a7b422cdSKonrad Rzeszutek Wilk } 104a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 105a7b422cdSKonrad Rzeszutek Wilk { 106a7b422cdSKonrad Rzeszutek Wilk off = 1; 107a7b422cdSKonrad Rzeszutek Wilk } 1083fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1091da177e4SLinus Torvalds 1104d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1114d5dcc42SViresh Kumar { 1120b981e70SViresh Kumar return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY); 1134d5dcc42SViresh Kumar } 1143f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1154d5dcc42SViresh Kumar 116944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 117944e9a03SViresh Kumar { 118944e9a03SViresh Kumar if (have_governor_per_policy()) 119944e9a03SViresh Kumar return &policy->kobj; 120944e9a03SViresh Kumar else 121944e9a03SViresh Kumar return cpufreq_global_kobject; 122944e9a03SViresh Kumar } 123944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 124944e9a03SViresh Kumar 1255a31d594SViresh Kumar struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu) 1265a31d594SViresh Kumar { 1275a31d594SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1285a31d594SViresh Kumar 1295a31d594SViresh Kumar return policy && !policy_is_inactive(policy) ? 1305a31d594SViresh Kumar policy->freq_table : NULL; 1315a31d594SViresh Kumar } 1325a31d594SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table); 1335a31d594SViresh Kumar 13472a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 13572a4ce34SViresh Kumar { 13672a4ce34SViresh Kumar u64 idle_time; 13772a4ce34SViresh Kumar u64 cur_wall_time; 13872a4ce34SViresh Kumar u64 busy_time; 13972a4ce34SViresh Kumar 14072a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 14172a4ce34SViresh Kumar 14272a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 14372a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 14472a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 14572a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 14672a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 14772a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 14872a4ce34SViresh Kumar 14972a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 15072a4ce34SViresh Kumar if (wall) 15172a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 15272a4ce34SViresh Kumar 15372a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 15472a4ce34SViresh Kumar } 15572a4ce34SViresh Kumar 15672a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 15772a4ce34SViresh Kumar { 15872a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 15972a4ce34SViresh Kumar 16072a4ce34SViresh Kumar if (idle_time == -1ULL) 16172a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 16272a4ce34SViresh Kumar else if (!io_busy) 16372a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 16472a4ce34SViresh Kumar 16572a4ce34SViresh Kumar return idle_time; 16672a4ce34SViresh Kumar } 16772a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 16872a4ce34SViresh Kumar 16970e9e778SViresh Kumar /* 17070e9e778SViresh Kumar * This is a generic cpufreq init() routine which can be used by cpufreq 17170e9e778SViresh Kumar * drivers of SMP systems. It will do following: 17270e9e778SViresh Kumar * - validate & show freq table passed 17370e9e778SViresh Kumar * - set policies transition latency 17470e9e778SViresh Kumar * - policy->cpus with all possible CPUs 17570e9e778SViresh Kumar */ 17670e9e778SViresh Kumar int cpufreq_generic_init(struct cpufreq_policy *policy, 17770e9e778SViresh Kumar struct cpufreq_frequency_table *table, 17870e9e778SViresh Kumar unsigned int transition_latency) 17970e9e778SViresh Kumar { 18070e9e778SViresh Kumar int ret; 18170e9e778SViresh Kumar 18270e9e778SViresh Kumar ret = cpufreq_table_validate_and_show(policy, table); 18370e9e778SViresh Kumar if (ret) { 18470e9e778SViresh Kumar pr_err("%s: invalid frequency table: %d\n", __func__, ret); 18570e9e778SViresh Kumar return ret; 18670e9e778SViresh Kumar } 18770e9e778SViresh Kumar 18870e9e778SViresh Kumar policy->cpuinfo.transition_latency = transition_latency; 18970e9e778SViresh Kumar 19070e9e778SViresh Kumar /* 19158405af6SShailendra Verma * The driver only supports the SMP configuration where all processors 19270e9e778SViresh Kumar * share the clock and voltage and clock. 19370e9e778SViresh Kumar */ 19470e9e778SViresh Kumar cpumask_setall(policy->cpus); 19570e9e778SViresh Kumar 19670e9e778SViresh Kumar return 0; 19770e9e778SViresh Kumar } 19870e9e778SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_init); 19970e9e778SViresh Kumar 2001f0bd44eSRafael J. Wysocki struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu) 201652ed95dSViresh Kumar { 202652ed95dSViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 203652ed95dSViresh Kumar 204988bed09SViresh Kumar return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL; 205988bed09SViresh Kumar } 2061f0bd44eSRafael J. Wysocki EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw); 207988bed09SViresh Kumar 208988bed09SViresh Kumar unsigned int cpufreq_generic_get(unsigned int cpu) 209988bed09SViresh Kumar { 210988bed09SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu); 211988bed09SViresh Kumar 212652ed95dSViresh Kumar if (!policy || IS_ERR(policy->clk)) { 213e837f9b5SJoe Perches pr_err("%s: No %s associated to cpu: %d\n", 214e837f9b5SJoe Perches __func__, policy ? "clk" : "policy", cpu); 215652ed95dSViresh Kumar return 0; 216652ed95dSViresh Kumar } 217652ed95dSViresh Kumar 218652ed95dSViresh Kumar return clk_get_rate(policy->clk) / 1000; 219652ed95dSViresh Kumar } 220652ed95dSViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_generic_get); 221652ed95dSViresh Kumar 22250e9c852SViresh Kumar /** 22350e9c852SViresh Kumar * cpufreq_cpu_get: returns policy for a cpu and marks it busy. 22450e9c852SViresh Kumar * 22550e9c852SViresh Kumar * @cpu: cpu to find policy for. 22650e9c852SViresh Kumar * 22750e9c852SViresh Kumar * This returns policy for 'cpu', returns NULL if it doesn't exist. 22850e9c852SViresh Kumar * It also increments the kobject reference count to mark it busy and so would 22950e9c852SViresh Kumar * require a corresponding call to cpufreq_cpu_put() to decrement it back. 23050e9c852SViresh Kumar * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be 23150e9c852SViresh Kumar * freed as that depends on the kobj count. 23250e9c852SViresh Kumar * 23350e9c852SViresh Kumar * Return: A valid policy on success, otherwise NULL on failure. 23450e9c852SViresh Kumar */ 2356eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 2361da177e4SLinus Torvalds { 2376eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 2381da177e4SLinus Torvalds unsigned long flags; 2391da177e4SLinus Torvalds 2401b947c90SViresh Kumar if (WARN_ON(cpu >= nr_cpu_ids)) 2416eed9404SViresh Kumar return NULL; 2426eed9404SViresh Kumar 2431da177e4SLinus Torvalds /* get the cpufreq driver */ 2440d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 2451da177e4SLinus Torvalds 2466eed9404SViresh Kumar if (cpufreq_driver) { 2471da177e4SLinus Torvalds /* get the CPU */ 248988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 2496eed9404SViresh Kumar if (policy) 2506eed9404SViresh Kumar kobject_get(&policy->kobj); 2516eed9404SViresh Kumar } 2526eed9404SViresh Kumar 2536eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2541da177e4SLinus Torvalds 2553a3e9e06SViresh Kumar return policy; 256a9144436SStephen Boyd } 2571da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2581da177e4SLinus Torvalds 25950e9c852SViresh Kumar /** 26050e9c852SViresh Kumar * cpufreq_cpu_put: Decrements the usage count of a policy 26150e9c852SViresh Kumar * 26250e9c852SViresh Kumar * @policy: policy earlier returned by cpufreq_cpu_get(). 26350e9c852SViresh Kumar * 26450e9c852SViresh Kumar * This decrements the kobject reference count incremented earlier by calling 26550e9c852SViresh Kumar * cpufreq_cpu_get(). 26650e9c852SViresh Kumar */ 2673a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 268a9144436SStephen Boyd { 2696eed9404SViresh Kumar kobject_put(&policy->kobj); 270a9144436SStephen Boyd } 2711da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2721da177e4SLinus Torvalds 2731da177e4SLinus Torvalds /********************************************************************* 2741da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2751da177e4SLinus Torvalds *********************************************************************/ 2761da177e4SLinus Torvalds 2771da177e4SLinus Torvalds /** 2781da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2791da177e4SLinus Torvalds * 2801da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2811da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2821da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2831da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2841da177e4SLinus Torvalds */ 28539c132eeSViresh Kumar static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 28639c132eeSViresh Kumar { 2871da177e4SLinus Torvalds #ifndef CONFIG_SMP 2881da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2891da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2901da177e4SLinus Torvalds 2911da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2921da177e4SLinus Torvalds return; 2931da177e4SLinus Torvalds 2941da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2951da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2961da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 297e837f9b5SJoe Perches pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n", 298e837f9b5SJoe Perches l_p_j_ref, l_p_j_ref_freq); 2991da177e4SLinus Torvalds } 3000b443eadSViresh Kumar if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) { 301e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 302e08f5f5bSGautham R Shenoy ci->new); 303e837f9b5SJoe Perches pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n", 304e837f9b5SJoe Perches loops_per_jiffy, ci->new); 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds #endif 30739c132eeSViresh Kumar } 3081da177e4SLinus Torvalds 3090956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 310b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 3131da177e4SLinus Torvalds 314d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 315d5aaffa9SDirk Brandewie return; 316d5aaffa9SDirk Brandewie 3171c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 3182d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 319e4472cb3SDave Jones state, freqs->new); 3201da177e4SLinus Torvalds 3211da177e4SLinus Torvalds switch (state) { 322e4472cb3SDave Jones 3231da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 324e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 325e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 326e4472cb3SDave Jones * "old frequency". 3271da177e4SLinus Torvalds */ 3281c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 329e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 330e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 331e837f9b5SJoe Perches pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n", 332e4472cb3SDave Jones freqs->old, policy->cur); 333e4472cb3SDave Jones freqs->old = policy->cur; 3341da177e4SLinus Torvalds } 3351da177e4SLinus Torvalds } 336b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 337e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3381da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3391da177e4SLinus Torvalds break; 340e4472cb3SDave Jones 3411da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3421da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 343e837f9b5SJoe Perches pr_debug("FREQ: %lu - CPU: %lu\n", 344e837f9b5SJoe Perches (unsigned long)freqs->new, (unsigned long)freqs->cpu); 34525e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 346b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 347e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 348e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 349e4472cb3SDave Jones policy->cur = freqs->new; 3501da177e4SLinus Torvalds break; 3511da177e4SLinus Torvalds } 3521da177e4SLinus Torvalds } 353bb176f7dSViresh Kumar 354b43a7ffbSViresh Kumar /** 355b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 356b43a7ffbSViresh Kumar * on frequency transition. 357b43a7ffbSViresh Kumar * 358b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 359b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 360b43a7ffbSViresh Kumar * external effects. 361b43a7ffbSViresh Kumar */ 362236a9800SViresh Kumar static void cpufreq_notify_transition(struct cpufreq_policy *policy, 363b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 364b43a7ffbSViresh Kumar { 365b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 366b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 367b43a7ffbSViresh Kumar } 3681da177e4SLinus Torvalds 369f7ba3b41SViresh Kumar /* Do post notifications when there are chances that transition has failed */ 370236a9800SViresh Kumar static void cpufreq_notify_post_transition(struct cpufreq_policy *policy, 371f7ba3b41SViresh Kumar struct cpufreq_freqs *freqs, int transition_failed) 372f7ba3b41SViresh Kumar { 373f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 374f7ba3b41SViresh Kumar if (!transition_failed) 375f7ba3b41SViresh Kumar return; 376f7ba3b41SViresh Kumar 377f7ba3b41SViresh Kumar swap(freqs->old, freqs->new); 378f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 379f7ba3b41SViresh Kumar cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE); 380f7ba3b41SViresh Kumar } 381f7ba3b41SViresh Kumar 38212478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_begin(struct cpufreq_policy *policy, 38312478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs) 38412478cf0SSrivatsa S. Bhat { 385ca654dc3SSrivatsa S. Bhat 386ca654dc3SSrivatsa S. Bhat /* 387ca654dc3SSrivatsa S. Bhat * Catch double invocations of _begin() which lead to self-deadlock. 388ca654dc3SSrivatsa S. Bhat * ASYNC_NOTIFICATION drivers are left out because the cpufreq core 389ca654dc3SSrivatsa S. Bhat * doesn't invoke _begin() on their behalf, and hence the chances of 390ca654dc3SSrivatsa S. Bhat * double invocations are very low. Moreover, there are scenarios 391ca654dc3SSrivatsa S. Bhat * where these checks can emit false-positive warnings in these 392ca654dc3SSrivatsa S. Bhat * drivers; so we avoid that by skipping them altogether. 393ca654dc3SSrivatsa S. Bhat */ 394ca654dc3SSrivatsa S. Bhat WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION) 395ca654dc3SSrivatsa S. Bhat && current == policy->transition_task); 396ca654dc3SSrivatsa S. Bhat 39712478cf0SSrivatsa S. Bhat wait: 39812478cf0SSrivatsa S. Bhat wait_event(policy->transition_wait, !policy->transition_ongoing); 39912478cf0SSrivatsa S. Bhat 40012478cf0SSrivatsa S. Bhat spin_lock(&policy->transition_lock); 40112478cf0SSrivatsa S. Bhat 40212478cf0SSrivatsa S. Bhat if (unlikely(policy->transition_ongoing)) { 40312478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 40412478cf0SSrivatsa S. Bhat goto wait; 40512478cf0SSrivatsa S. Bhat } 40612478cf0SSrivatsa S. Bhat 40712478cf0SSrivatsa S. Bhat policy->transition_ongoing = true; 408ca654dc3SSrivatsa S. Bhat policy->transition_task = current; 40912478cf0SSrivatsa S. Bhat 41012478cf0SSrivatsa S. Bhat spin_unlock(&policy->transition_lock); 41112478cf0SSrivatsa S. Bhat 41212478cf0SSrivatsa S. Bhat cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE); 41312478cf0SSrivatsa S. Bhat } 41412478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin); 41512478cf0SSrivatsa S. Bhat 41612478cf0SSrivatsa S. Bhat void cpufreq_freq_transition_end(struct cpufreq_policy *policy, 41712478cf0SSrivatsa S. Bhat struct cpufreq_freqs *freqs, int transition_failed) 41812478cf0SSrivatsa S. Bhat { 41912478cf0SSrivatsa S. Bhat if (unlikely(WARN_ON(!policy->transition_ongoing))) 42012478cf0SSrivatsa S. Bhat return; 42112478cf0SSrivatsa S. Bhat 42212478cf0SSrivatsa S. Bhat cpufreq_notify_post_transition(policy, freqs, transition_failed); 42312478cf0SSrivatsa S. Bhat 42412478cf0SSrivatsa S. Bhat policy->transition_ongoing = false; 425ca654dc3SSrivatsa S. Bhat policy->transition_task = NULL; 42612478cf0SSrivatsa S. Bhat 42712478cf0SSrivatsa S. Bhat wake_up(&policy->transition_wait); 42812478cf0SSrivatsa S. Bhat } 42912478cf0SSrivatsa S. Bhat EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end); 43012478cf0SSrivatsa S. Bhat 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds /********************************************************************* 4331da177e4SLinus Torvalds * SYSFS INTERFACE * 4341da177e4SLinus Torvalds *********************************************************************/ 4358a5c74a1SRashika Kheria static ssize_t show_boost(struct kobject *kobj, 4366f19efc0SLukasz Majewski struct attribute *attr, char *buf) 4376f19efc0SLukasz Majewski { 4386f19efc0SLukasz Majewski return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled); 4396f19efc0SLukasz Majewski } 4406f19efc0SLukasz Majewski 4416f19efc0SLukasz Majewski static ssize_t store_boost(struct kobject *kobj, struct attribute *attr, 4426f19efc0SLukasz Majewski const char *buf, size_t count) 4436f19efc0SLukasz Majewski { 4446f19efc0SLukasz Majewski int ret, enable; 4456f19efc0SLukasz Majewski 4466f19efc0SLukasz Majewski ret = sscanf(buf, "%d", &enable); 4476f19efc0SLukasz Majewski if (ret != 1 || enable < 0 || enable > 1) 4486f19efc0SLukasz Majewski return -EINVAL; 4496f19efc0SLukasz Majewski 4506f19efc0SLukasz Majewski if (cpufreq_boost_trigger_state(enable)) { 451e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST!\n", 452e837f9b5SJoe Perches __func__, enable ? "enable" : "disable"); 4536f19efc0SLukasz Majewski return -EINVAL; 4546f19efc0SLukasz Majewski } 4556f19efc0SLukasz Majewski 456e837f9b5SJoe Perches pr_debug("%s: cpufreq BOOST %s\n", 457e837f9b5SJoe Perches __func__, enable ? "enabled" : "disabled"); 4586f19efc0SLukasz Majewski 4596f19efc0SLukasz Majewski return count; 4606f19efc0SLukasz Majewski } 4616f19efc0SLukasz Majewski define_one_global_rw(boost); 4621da177e4SLinus Torvalds 46342f91fa1SViresh Kumar static struct cpufreq_governor *find_governor(const char *str_governor) 4643bcb09a3SJeremy Fitzhardinge { 4653bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 4663bcb09a3SJeremy Fitzhardinge 467f7b27061SViresh Kumar for_each_governor(t) 4687c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 4693bcb09a3SJeremy Fitzhardinge return t; 4703bcb09a3SJeremy Fitzhardinge 4713bcb09a3SJeremy Fitzhardinge return NULL; 4723bcb09a3SJeremy Fitzhardinge } 4733bcb09a3SJeremy Fitzhardinge 4741da177e4SLinus Torvalds /** 4751da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 4761da177e4SLinus Torvalds */ 4771da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 4781da177e4SLinus Torvalds struct cpufreq_governor **governor) 4791da177e4SLinus Torvalds { 4803bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 4813bcb09a3SJeremy Fitzhardinge 4821c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 4837c4f4539SRasmus Villemoes if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 4841da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 4853bcb09a3SJeremy Fitzhardinge err = 0; 4867c4f4539SRasmus Villemoes } else if (!strncasecmp(str_governor, "powersave", 487e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 4881da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 4893bcb09a3SJeremy Fitzhardinge err = 0; 4901da177e4SLinus Torvalds } 4912e1cc3a5SViresh Kumar } else { 4921da177e4SLinus Torvalds struct cpufreq_governor *t; 4933bcb09a3SJeremy Fitzhardinge 4943fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 4953bcb09a3SJeremy Fitzhardinge 49642f91fa1SViresh Kumar t = find_governor(str_governor); 4973bcb09a3SJeremy Fitzhardinge 498ea714970SJeremy Fitzhardinge if (t == NULL) { 499ea714970SJeremy Fitzhardinge int ret; 500ea714970SJeremy Fitzhardinge 501ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5021a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 503ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 504ea714970SJeremy Fitzhardinge 505ea714970SJeremy Fitzhardinge if (ret == 0) 50642f91fa1SViresh Kumar t = find_governor(str_governor); 507ea714970SJeremy Fitzhardinge } 508ea714970SJeremy Fitzhardinge 5093bcb09a3SJeremy Fitzhardinge if (t != NULL) { 5101da177e4SLinus Torvalds *governor = t; 5113bcb09a3SJeremy Fitzhardinge err = 0; 5121da177e4SLinus Torvalds } 5133bcb09a3SJeremy Fitzhardinge 5143bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 5151da177e4SLinus Torvalds } 5163bcb09a3SJeremy Fitzhardinge return err; 5171da177e4SLinus Torvalds } 5181da177e4SLinus Torvalds 5191da177e4SLinus Torvalds /** 520e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 521e08f5f5bSGautham R Shenoy * print out cpufreq information 5221da177e4SLinus Torvalds * 5231da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 5241da177e4SLinus Torvalds * "unsigned int". 5251da177e4SLinus Torvalds */ 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds #define show_one(file_name, object) \ 5281da177e4SLinus Torvalds static ssize_t show_##file_name \ 5291da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 5301da177e4SLinus Torvalds { \ 5311da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds 5341da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 5351da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 536ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 5371da177e4SLinus Torvalds show_one(scaling_min_freq, min); 5381da177e4SLinus Torvalds show_one(scaling_max_freq, max); 539c034b02eSDirk Brandewie 54009347b29SViresh Kumar static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf) 541c034b02eSDirk Brandewie { 542c034b02eSDirk Brandewie ssize_t ret; 543c034b02eSDirk Brandewie 544c034b02eSDirk Brandewie if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 545c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu)); 546c034b02eSDirk Brandewie else 547c034b02eSDirk Brandewie ret = sprintf(buf, "%u\n", policy->cur); 548c034b02eSDirk Brandewie return ret; 549c034b02eSDirk Brandewie } 5501da177e4SLinus Torvalds 551037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 5523a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 5537970e08bSThomas Renninger 5541da177e4SLinus Torvalds /** 5551da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 5561da177e4SLinus Torvalds */ 5571da177e4SLinus Torvalds #define store_one(file_name, object) \ 5581da177e4SLinus Torvalds static ssize_t store_##file_name \ 5591da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 5601da177e4SLinus Torvalds { \ 561619c144cSVince Hsu int ret, temp; \ 5621da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 5631da177e4SLinus Torvalds \ 5648fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); \ 5651da177e4SLinus Torvalds \ 5661da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 5671da177e4SLinus Torvalds if (ret != 1) \ 5681da177e4SLinus Torvalds return -EINVAL; \ 5691da177e4SLinus Torvalds \ 570619c144cSVince Hsu temp = new_policy.object; \ 571037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); \ 572619c144cSVince Hsu if (!ret) \ 573619c144cSVince Hsu policy->user_policy.object = temp; \ 5741da177e4SLinus Torvalds \ 5751da177e4SLinus Torvalds return ret ? ret : count; \ 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds 5781da177e4SLinus Torvalds store_one(scaling_min_freq, min); 5791da177e4SLinus Torvalds store_one(scaling_max_freq, max); 5801da177e4SLinus Torvalds 5811da177e4SLinus Torvalds /** 5821da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 5831da177e4SLinus Torvalds */ 584e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 585e08f5f5bSGautham R Shenoy char *buf) 5861da177e4SLinus Torvalds { 587d92d50a4SViresh Kumar unsigned int cur_freq = __cpufreq_get(policy); 5881da177e4SLinus Torvalds if (!cur_freq) 5891da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 5901da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 5911da177e4SLinus Torvalds } 5921da177e4SLinus Torvalds 5931da177e4SLinus Torvalds /** 5941da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 5951da177e4SLinus Torvalds */ 596905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 5991da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 6001da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 6011da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 6021da177e4SLinus Torvalds else if (policy->governor) 6034b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 60429464f28SDave Jones policy->governor->name); 6051da177e4SLinus Torvalds return -EINVAL; 6061da177e4SLinus Torvalds } 6071da177e4SLinus Torvalds 6081da177e4SLinus Torvalds /** 6091da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 6101da177e4SLinus Torvalds */ 6111da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 6121da177e4SLinus Torvalds const char *buf, size_t count) 6131da177e4SLinus Torvalds { 6145136fa56SSrivatsa S. Bhat int ret; 6151da177e4SLinus Torvalds char str_governor[16]; 6161da177e4SLinus Torvalds struct cpufreq_policy new_policy; 6171da177e4SLinus Torvalds 6188fa5b631SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 6191da177e4SLinus Torvalds 6201da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 6211da177e4SLinus Torvalds if (ret != 1) 6221da177e4SLinus Torvalds return -EINVAL; 6231da177e4SLinus Torvalds 624e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 625e08f5f5bSGautham R Shenoy &new_policy.governor)) 6261da177e4SLinus Torvalds return -EINVAL; 6271da177e4SLinus Torvalds 628037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 62988dc4384SViresh Kumar return ret ? ret : count; 6301da177e4SLinus Torvalds } 6311da177e4SLinus Torvalds 6321da177e4SLinus Torvalds /** 6331da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 6341da177e4SLinus Torvalds */ 6351da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 6361da177e4SLinus Torvalds { 6371c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 6381da177e4SLinus Torvalds } 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds /** 6411da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 6421da177e4SLinus Torvalds */ 6431da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 6441da177e4SLinus Torvalds char *buf) 6451da177e4SLinus Torvalds { 6461da177e4SLinus Torvalds ssize_t i = 0; 6471da177e4SLinus Torvalds struct cpufreq_governor *t; 6481da177e4SLinus Torvalds 6499c0ebcf7SViresh Kumar if (!has_target()) { 6501da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 6511da177e4SLinus Torvalds goto out; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds 654f7b27061SViresh Kumar for_each_governor(t) { 65529464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 65629464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 6571da177e4SLinus Torvalds goto out; 6584b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 6591da177e4SLinus Torvalds } 6601da177e4SLinus Torvalds out: 6611da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6621da177e4SLinus Torvalds return i; 6631da177e4SLinus Torvalds } 664e8628dd0SDarrick J. Wong 665f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds ssize_t i = 0; 6681da177e4SLinus Torvalds unsigned int cpu; 6691da177e4SLinus Torvalds 670835481d9SRusty Russell for_each_cpu(cpu, mask) { 6711da177e4SLinus Torvalds if (i) 6721da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 6731da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 6741da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 6751da177e4SLinus Torvalds break; 6761da177e4SLinus Torvalds } 6771da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 6781da177e4SLinus Torvalds return i; 6791da177e4SLinus Torvalds } 680f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 6811da177e4SLinus Torvalds 682e8628dd0SDarrick J. Wong /** 683e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 684e8628dd0SDarrick J. Wong * hw coordination is in use 685e8628dd0SDarrick J. Wong */ 686e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 687e8628dd0SDarrick J. Wong { 688f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 689e8628dd0SDarrick J. Wong } 690e8628dd0SDarrick J. Wong 691e8628dd0SDarrick J. Wong /** 692e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 693e8628dd0SDarrick J. Wong */ 694e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 695e8628dd0SDarrick J. Wong { 696f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 697e8628dd0SDarrick J. Wong } 698e8628dd0SDarrick J. Wong 6999e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 7009e76988eSVenki Pallipadi const char *buf, size_t count) 7019e76988eSVenki Pallipadi { 7029e76988eSVenki Pallipadi unsigned int freq = 0; 7039e76988eSVenki Pallipadi unsigned int ret; 7049e76988eSVenki Pallipadi 705879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 7069e76988eSVenki Pallipadi return -EINVAL; 7079e76988eSVenki Pallipadi 7089e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 7099e76988eSVenki Pallipadi if (ret != 1) 7109e76988eSVenki Pallipadi return -EINVAL; 7119e76988eSVenki Pallipadi 7129e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 7139e76988eSVenki Pallipadi 7149e76988eSVenki Pallipadi return count; 7159e76988eSVenki Pallipadi } 7169e76988eSVenki Pallipadi 7179e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 7189e76988eSVenki Pallipadi { 719879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 7209e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 7219e76988eSVenki Pallipadi 7229e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 7239e76988eSVenki Pallipadi } 7241da177e4SLinus Torvalds 725e2f74f35SThomas Renninger /** 7268bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 727e2f74f35SThomas Renninger */ 728e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 729e2f74f35SThomas Renninger { 730e2f74f35SThomas Renninger unsigned int limit; 731e2f74f35SThomas Renninger int ret; 7321c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 7331c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 734e2f74f35SThomas Renninger if (!ret) 735e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 736e2f74f35SThomas Renninger } 737e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 738e2f74f35SThomas Renninger } 739e2f74f35SThomas Renninger 7406dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 7416dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 7426dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 7436dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 7446dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 7456dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 7466dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 7476dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 7486dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 7496dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 7506dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 7516dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 7526dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 7536dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 7541da177e4SLinus Torvalds 7551da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 7561da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 7571da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 758ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 7591da177e4SLinus Torvalds &scaling_min_freq.attr, 7601da177e4SLinus Torvalds &scaling_max_freq.attr, 7611da177e4SLinus Torvalds &affected_cpus.attr, 762e8628dd0SDarrick J. Wong &related_cpus.attr, 7631da177e4SLinus Torvalds &scaling_governor.attr, 7641da177e4SLinus Torvalds &scaling_driver.attr, 7651da177e4SLinus Torvalds &scaling_available_governors.attr, 7669e76988eSVenki Pallipadi &scaling_setspeed.attr, 7671da177e4SLinus Torvalds NULL 7681da177e4SLinus Torvalds }; 7691da177e4SLinus Torvalds 7701da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 7711da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 7721da177e4SLinus Torvalds 7731da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 7741da177e4SLinus Torvalds { 7751da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7761da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 7771b750e3bSViresh Kumar ssize_t ret; 7786eed9404SViresh Kumar 779ad7722daSviresh kumar down_read(&policy->rwsem); 780e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 781ad7722daSviresh kumar up_read(&policy->rwsem); 7821b750e3bSViresh Kumar 7831da177e4SLinus Torvalds return ret; 7841da177e4SLinus Torvalds } 7851da177e4SLinus Torvalds 7861da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 7871da177e4SLinus Torvalds const char *buf, size_t count) 7881da177e4SLinus Torvalds { 7891da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7901da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 791a07530b4SDave Jones ssize_t ret = -EINVAL; 7926eed9404SViresh Kumar 7934f750c93SSrivatsa S. Bhat get_online_cpus(); 7944f750c93SSrivatsa S. Bhat 7956541aef0SRafael J. Wysocki if (cpu_online(policy->cpu)) { 796ad7722daSviresh kumar down_write(&policy->rwsem); 797e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 798ad7722daSviresh kumar up_write(&policy->rwsem); 7996541aef0SRafael J. Wysocki } 8006541aef0SRafael J. Wysocki 8014f750c93SSrivatsa S. Bhat put_online_cpus(); 8024f750c93SSrivatsa S. Bhat 8031da177e4SLinus Torvalds return ret; 8041da177e4SLinus Torvalds } 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 8071da177e4SLinus Torvalds { 8081da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 8092d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 8101da177e4SLinus Torvalds complete(&policy->kobj_unregister); 8111da177e4SLinus Torvalds } 8121da177e4SLinus Torvalds 81352cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 8141da177e4SLinus Torvalds .show = show, 8151da177e4SLinus Torvalds .store = store, 8161da177e4SLinus Torvalds }; 8171da177e4SLinus Torvalds 8181da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 8191da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 8201da177e4SLinus Torvalds .default_attrs = default_attrs, 8211da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 8221da177e4SLinus Torvalds }; 8231da177e4SLinus Torvalds 82487549141SViresh Kumar static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) 82587549141SViresh Kumar { 82687549141SViresh Kumar struct device *cpu_dev; 82787549141SViresh Kumar 82887549141SViresh Kumar pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu); 82987549141SViresh Kumar 83087549141SViresh Kumar if (!policy) 83187549141SViresh Kumar return 0; 83287549141SViresh Kumar 83387549141SViresh Kumar cpu_dev = get_cpu_device(cpu); 83487549141SViresh Kumar if (WARN_ON(!cpu_dev)) 83587549141SViresh Kumar return 0; 83687549141SViresh Kumar 83787549141SViresh Kumar return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq"); 83887549141SViresh Kumar } 83987549141SViresh Kumar 84087549141SViresh Kumar static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu) 84187549141SViresh Kumar { 84287549141SViresh Kumar struct device *cpu_dev; 84387549141SViresh Kumar 84487549141SViresh Kumar pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu); 84587549141SViresh Kumar 84687549141SViresh Kumar cpu_dev = get_cpu_device(cpu); 84787549141SViresh Kumar if (WARN_ON(!cpu_dev)) 84887549141SViresh Kumar return; 84987549141SViresh Kumar 85087549141SViresh Kumar sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 85187549141SViresh Kumar } 85287549141SViresh Kumar 85387549141SViresh Kumar /* Add/remove symlinks for all related CPUs */ 854308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 85519d6f7ecSDave Jones { 85619d6f7ecSDave Jones unsigned int j; 85719d6f7ecSDave Jones int ret = 0; 85819d6f7ecSDave Jones 85987549141SViresh Kumar /* Some related CPUs might not be present (physically hotplugged) */ 860559ed407SRafael J. Wysocki for_each_cpu(j, policy->real_cpus) { 86187549141SViresh Kumar ret = add_cpu_dev_symlink(policy, j); 86271c3461eSRafael J. Wysocki if (ret) 86371c3461eSRafael J. Wysocki break; 86419d6f7ecSDave Jones } 86587549141SViresh Kumar 86619d6f7ecSDave Jones return ret; 86719d6f7ecSDave Jones } 86819d6f7ecSDave Jones 86987549141SViresh Kumar static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy) 87087549141SViresh Kumar { 87187549141SViresh Kumar unsigned int j; 87287549141SViresh Kumar 87387549141SViresh Kumar /* Some related CPUs might not be present (physically hotplugged) */ 87496bdda61SViresh Kumar for_each_cpu(j, policy->real_cpus) 87587549141SViresh Kumar remove_cpu_dev_symlink(policy, j); 87687549141SViresh Kumar } 87787549141SViresh Kumar 878d9612a49SRafael J. Wysocki static int cpufreq_add_dev_interface(struct cpufreq_policy *policy) 879909a694eSDave Jones { 880909a694eSDave Jones struct freq_attr **drv_attr; 881909a694eSDave Jones int ret = 0; 882909a694eSDave Jones 883909a694eSDave Jones /* set up files for this cpu device */ 8841c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 885f13f1184SViresh Kumar while (drv_attr && *drv_attr) { 886909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 887909a694eSDave Jones if (ret) 8886d4e81edSTomeu Vizoso return ret; 889909a694eSDave Jones drv_attr++; 890909a694eSDave Jones } 8911c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 892909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 893909a694eSDave Jones if (ret) 8946d4e81edSTomeu Vizoso return ret; 895909a694eSDave Jones } 896c034b02eSDirk Brandewie 897909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 898909a694eSDave Jones if (ret) 8996d4e81edSTomeu Vizoso return ret; 900c034b02eSDirk Brandewie 9011c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 902e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 903e2f74f35SThomas Renninger if (ret) 9046d4e81edSTomeu Vizoso return ret; 905e2f74f35SThomas Renninger } 906909a694eSDave Jones 9076d4e81edSTomeu Vizoso return cpufreq_add_dev_symlink(policy); 908e18f1682SSrivatsa S. Bhat } 909e18f1682SSrivatsa S. Bhat 910de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_default_governor(void) 911de1df26bSRafael J. Wysocki { 912de1df26bSRafael J. Wysocki return NULL; 913de1df26bSRafael J. Wysocki } 914de1df26bSRafael J. Wysocki 9157f0fa40fSViresh Kumar static int cpufreq_init_policy(struct cpufreq_policy *policy) 916e18f1682SSrivatsa S. Bhat { 9176e2c89d1Sviresh kumar struct cpufreq_governor *gov = NULL; 918e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 919e18f1682SSrivatsa S. Bhat 920d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 921a27a9ab7SJason Baron 9226e2c89d1Sviresh kumar /* Update governor of new_policy to the governor used before hotplug */ 9234573237bSViresh Kumar gov = find_governor(policy->last_governor); 924de1df26bSRafael J. Wysocki if (gov) { 9256e2c89d1Sviresh kumar pr_debug("Restoring governor %s for cpu %d\n", 9266e2c89d1Sviresh kumar policy->governor->name, policy->cpu); 927de1df26bSRafael J. Wysocki } else { 928de1df26bSRafael J. Wysocki gov = cpufreq_default_governor(); 929de1df26bSRafael J. Wysocki if (!gov) 930de1df26bSRafael J. Wysocki return -ENODATA; 931de1df26bSRafael J. Wysocki } 9326e2c89d1Sviresh kumar 9336e2c89d1Sviresh kumar new_policy.governor = gov; 9346e2c89d1Sviresh kumar 93569030dd1SSrinivas Pandruvada /* Use the default policy if there is no last_policy. */ 93669030dd1SSrinivas Pandruvada if (cpufreq_driver->setpolicy) { 93769030dd1SSrinivas Pandruvada if (policy->last_policy) 93869030dd1SSrinivas Pandruvada new_policy.policy = policy->last_policy; 93969030dd1SSrinivas Pandruvada else 94069030dd1SSrinivas Pandruvada cpufreq_parse_governor(gov->name, &new_policy.policy, 94169030dd1SSrinivas Pandruvada NULL); 94269030dd1SSrinivas Pandruvada } 943ecf7e461SDave Jones /* set default policy */ 9447f0fa40fSViresh Kumar return cpufreq_set_policy(policy, &new_policy); 945909a694eSDave Jones } 946909a694eSDave Jones 947d9612a49SRafael J. Wysocki static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 948fcf80582SViresh Kumar { 9499c0ebcf7SViresh Kumar int ret = 0; 950fcf80582SViresh Kumar 951bb29ae15SViresh Kumar /* Has this CPU been taken care of already? */ 952bb29ae15SViresh Kumar if (cpumask_test_cpu(cpu, policy->cpus)) 953bb29ae15SViresh Kumar return 0; 954bb29ae15SViresh Kumar 95549f18560SViresh Kumar down_write(&policy->rwsem); 9569c0ebcf7SViresh Kumar if (has_target()) { 957a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 9583de9bdebSViresh Kumar if (ret) { 9593de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 96049f18560SViresh Kumar goto unlock; 9613de9bdebSViresh Kumar } 9623de9bdebSViresh Kumar } 963fcf80582SViresh Kumar 964fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 9652eaa3e2dSViresh Kumar 9669c0ebcf7SViresh Kumar if (has_target()) { 967a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 968e5c87b76SStratos Karafotis if (!ret) 969a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 970e5c87b76SStratos Karafotis 97149f18560SViresh Kumar if (ret) 9723de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 973820c6ca2SViresh Kumar } 974fcf80582SViresh Kumar 97549f18560SViresh Kumar unlock: 97649f18560SViresh Kumar up_write(&policy->rwsem); 977fcf80582SViresh Kumar return ret; 978fcf80582SViresh Kumar } 979fcf80582SViresh Kumar 98011eb69b9SViresh Kumar static void handle_update(struct work_struct *work) 98111eb69b9SViresh Kumar { 98211eb69b9SViresh Kumar struct cpufreq_policy *policy = 98311eb69b9SViresh Kumar container_of(work, struct cpufreq_policy, update); 98411eb69b9SViresh Kumar unsigned int cpu = policy->cpu; 98511eb69b9SViresh Kumar pr_debug("handle_update for cpu %u called\n", cpu); 98611eb69b9SViresh Kumar cpufreq_update_policy(cpu); 987fcf80582SViresh Kumar } 9881da177e4SLinus Torvalds 989a34e63b1SRafael J. Wysocki static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu) 9908414809cSSrivatsa S. Bhat { 991a34e63b1SRafael J. Wysocki struct device *dev = get_cpu_device(cpu); 992e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 993edd4a893SViresh Kumar int ret; 994e9698cc5SSrivatsa S. Bhat 995a34e63b1SRafael J. Wysocki if (WARN_ON(!dev)) 996a34e63b1SRafael J. Wysocki return NULL; 997a34e63b1SRafael J. Wysocki 998e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 999e9698cc5SSrivatsa S. Bhat if (!policy) 1000e9698cc5SSrivatsa S. Bhat return NULL; 1001e9698cc5SSrivatsa S. Bhat 1002e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1003e9698cc5SSrivatsa S. Bhat goto err_free_policy; 1004e9698cc5SSrivatsa S. Bhat 1005e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1006e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 1007e9698cc5SSrivatsa S. Bhat 1008559ed407SRafael J. Wysocki if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL)) 1009559ed407SRafael J. Wysocki goto err_free_rcpumask; 1010559ed407SRafael J. Wysocki 1011edd4a893SViresh Kumar ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 1012edd4a893SViresh Kumar cpufreq_global_kobject, "policy%u", cpu); 1013edd4a893SViresh Kumar if (ret) { 1014edd4a893SViresh Kumar pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret); 1015edd4a893SViresh Kumar goto err_free_real_cpus; 1016edd4a893SViresh Kumar } 1017edd4a893SViresh Kumar 1018c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 1019ad7722daSviresh kumar init_rwsem(&policy->rwsem); 102012478cf0SSrivatsa S. Bhat spin_lock_init(&policy->transition_lock); 102112478cf0SSrivatsa S. Bhat init_waitqueue_head(&policy->transition_wait); 1022818c5712SViresh Kumar init_completion(&policy->kobj_unregister); 1023818c5712SViresh Kumar INIT_WORK(&policy->update, handle_update); 1024ad7722daSviresh kumar 1025a34e63b1SRafael J. Wysocki policy->cpu = cpu; 1026e9698cc5SSrivatsa S. Bhat return policy; 1027e9698cc5SSrivatsa S. Bhat 1028edd4a893SViresh Kumar err_free_real_cpus: 1029edd4a893SViresh Kumar free_cpumask_var(policy->real_cpus); 10302fc3384dSViresh Kumar err_free_rcpumask: 10312fc3384dSViresh Kumar free_cpumask_var(policy->related_cpus); 1032e9698cc5SSrivatsa S. Bhat err_free_cpumask: 1033e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1034e9698cc5SSrivatsa S. Bhat err_free_policy: 1035e9698cc5SSrivatsa S. Bhat kfree(policy); 1036e9698cc5SSrivatsa S. Bhat 1037e9698cc5SSrivatsa S. Bhat return NULL; 1038e9698cc5SSrivatsa S. Bhat } 1039e9698cc5SSrivatsa S. Bhat 10402fc3384dSViresh Kumar static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy, bool notify) 104142f921a6SViresh Kumar { 104242f921a6SViresh Kumar struct kobject *kobj; 104342f921a6SViresh Kumar struct completion *cmp; 104442f921a6SViresh Kumar 10452fc3384dSViresh Kumar if (notify) 1046fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1047fcd7af91SViresh Kumar CPUFREQ_REMOVE_POLICY, policy); 1048fcd7af91SViresh Kumar 104987549141SViresh Kumar down_write(&policy->rwsem); 105087549141SViresh Kumar cpufreq_remove_dev_symlink(policy); 105142f921a6SViresh Kumar kobj = &policy->kobj; 105242f921a6SViresh Kumar cmp = &policy->kobj_unregister; 105387549141SViresh Kumar up_write(&policy->rwsem); 105442f921a6SViresh Kumar kobject_put(kobj); 105542f921a6SViresh Kumar 105642f921a6SViresh Kumar /* 105742f921a6SViresh Kumar * We need to make sure that the underlying kobj is 105842f921a6SViresh Kumar * actually not referenced anymore by anybody before we 105942f921a6SViresh Kumar * proceed with unloading. 106042f921a6SViresh Kumar */ 106142f921a6SViresh Kumar pr_debug("waiting for dropping of refcount\n"); 106242f921a6SViresh Kumar wait_for_completion(cmp); 106342f921a6SViresh Kumar pr_debug("wait complete\n"); 106442f921a6SViresh Kumar } 106542f921a6SViresh Kumar 10663654c5ccSViresh Kumar static void cpufreq_policy_free(struct cpufreq_policy *policy, bool notify) 1067e9698cc5SSrivatsa S. Bhat { 1068988bed09SViresh Kumar unsigned long flags; 1069988bed09SViresh Kumar int cpu; 1070988bed09SViresh Kumar 1071988bed09SViresh Kumar /* Remove policy from list */ 1072988bed09SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1073988bed09SViresh Kumar list_del(&policy->policy_list); 1074988bed09SViresh Kumar 1075988bed09SViresh Kumar for_each_cpu(cpu, policy->related_cpus) 1076988bed09SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 1077988bed09SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1078988bed09SViresh Kumar 10793654c5ccSViresh Kumar cpufreq_policy_put_kobj(policy, notify); 1080559ed407SRafael J. Wysocki free_cpumask_var(policy->real_cpus); 1081e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 1082e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 1083e9698cc5SSrivatsa S. Bhat kfree(policy); 1084e9698cc5SSrivatsa S. Bhat } 1085e9698cc5SSrivatsa S. Bhat 10860b275352SRafael J. Wysocki static int cpufreq_online(unsigned int cpu) 10871da177e4SLinus Torvalds { 10887f0c020aSViresh Kumar struct cpufreq_policy *policy; 1089194d99c7SRafael J. Wysocki bool new_policy; 10901da177e4SLinus Torvalds unsigned long flags; 10910b275352SRafael J. Wysocki unsigned int j; 10920b275352SRafael J. Wysocki int ret; 1093c32b6b8eSAshok Raj 10940b275352SRafael J. Wysocki pr_debug("%s: bringing CPU%u online\n", __func__, cpu); 10956eed9404SViresh Kumar 1096bb29ae15SViresh Kumar /* Check if this CPU already has a policy to manage it */ 10979104bb26SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 109811ce707eSRafael J. Wysocki if (policy) { 10999104bb26SViresh Kumar WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus)); 110011ce707eSRafael J. Wysocki if (!policy_is_inactive(policy)) 1101d9612a49SRafael J. Wysocki return cpufreq_add_policy_cpu(policy, cpu); 11021da177e4SLinus Torvalds 110311ce707eSRafael J. Wysocki /* This is the only online CPU for the policy. Start over. */ 1104194d99c7SRafael J. Wysocki new_policy = false; 110511ce707eSRafael J. Wysocki down_write(&policy->rwsem); 110611ce707eSRafael J. Wysocki policy->cpu = cpu; 110711ce707eSRafael J. Wysocki policy->governor = NULL; 110811ce707eSRafael J. Wysocki up_write(&policy->rwsem); 110911ce707eSRafael J. Wysocki } else { 1110194d99c7SRafael J. Wysocki new_policy = true; 1111a34e63b1SRafael J. Wysocki policy = cpufreq_policy_alloc(cpu); 1112059019a3SDave Jones if (!policy) 1113d4d854d6SRafael J. Wysocki return -ENOMEM; 111472368d12SRafael J. Wysocki } 11150d66b91eSSrivatsa S. Bhat 1116835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 11171da177e4SLinus Torvalds 11181da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 11191da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 11201da177e4SLinus Torvalds */ 11211c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 11221da177e4SLinus Torvalds if (ret) { 11232d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 11248101f997SViresh Kumar goto out_free_policy; 11251da177e4SLinus Torvalds } 1126643ae6e8SViresh Kumar 11276d4e81edSTomeu Vizoso down_write(&policy->rwsem); 11286d4e81edSTomeu Vizoso 1129194d99c7SRafael J. Wysocki if (new_policy) { 11304d1f3a5bSRafael J. Wysocki /* related_cpus should at least include policy->cpus. */ 11310998a03aSViresh Kumar cpumask_copy(policy->related_cpus, policy->cpus); 11324d1f3a5bSRafael J. Wysocki /* Remember CPUs present at the policy creation time. */ 1133559ed407SRafael J. Wysocki cpumask_and(policy->real_cpus, policy->cpus, cpu_present_mask); 11344d1f3a5bSRafael J. Wysocki } 1135559ed407SRafael J. Wysocki 11365a7e56a5SViresh Kumar /* 11375a7e56a5SViresh Kumar * affected cpus must always be the one, which are online. We aren't 11385a7e56a5SViresh Kumar * managing offline cpus here. 11395a7e56a5SViresh Kumar */ 11405a7e56a5SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 11415a7e56a5SViresh Kumar 1142194d99c7SRafael J. Wysocki if (new_policy) { 11435a7e56a5SViresh Kumar policy->user_policy.min = policy->min; 11445a7e56a5SViresh Kumar policy->user_policy.max = policy->max; 11456d4e81edSTomeu Vizoso 1146652ed95dSViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 1147988bed09SViresh Kumar for_each_cpu(j, policy->related_cpus) 1148652ed95dSViresh Kumar per_cpu(cpufreq_cpu_data, j) = policy; 1149652ed95dSViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1150988bed09SViresh Kumar } 1151652ed95dSViresh Kumar 11522ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 1153da60ce9fSViresh Kumar policy->cur = cpufreq_driver->get(policy->cpu); 1154da60ce9fSViresh Kumar if (!policy->cur) { 1155da60ce9fSViresh Kumar pr_err("%s: ->get() failed\n", __func__); 11568101f997SViresh Kumar goto out_exit_policy; 1157da60ce9fSViresh Kumar } 1158da60ce9fSViresh Kumar } 1159da60ce9fSViresh Kumar 1160d3916691SViresh Kumar /* 1161d3916691SViresh Kumar * Sometimes boot loaders set CPU frequency to a value outside of 1162d3916691SViresh Kumar * frequency table present with cpufreq core. In such cases CPU might be 1163d3916691SViresh Kumar * unstable if it has to run on that frequency for long duration of time 1164d3916691SViresh Kumar * and so its better to set it to a frequency which is specified in 1165d3916691SViresh Kumar * freq-table. This also makes cpufreq stats inconsistent as 1166d3916691SViresh Kumar * cpufreq-stats would fail to register because current frequency of CPU 1167d3916691SViresh Kumar * isn't found in freq-table. 1168d3916691SViresh Kumar * 1169d3916691SViresh Kumar * Because we don't want this change to effect boot process badly, we go 1170d3916691SViresh Kumar * for the next freq which is >= policy->cur ('cur' must be set by now, 1171d3916691SViresh Kumar * otherwise we will end up setting freq to lowest of the table as 'cur' 1172d3916691SViresh Kumar * is initialized to zero). 1173d3916691SViresh Kumar * 1174d3916691SViresh Kumar * We are passing target-freq as "policy->cur - 1" otherwise 1175d3916691SViresh Kumar * __cpufreq_driver_target() would simply fail, as policy->cur will be 1176d3916691SViresh Kumar * equal to target-freq. 1177d3916691SViresh Kumar */ 1178d3916691SViresh Kumar if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK) 1179d3916691SViresh Kumar && has_target()) { 1180d3916691SViresh Kumar /* Are we running at unknown frequency ? */ 1181d3916691SViresh Kumar ret = cpufreq_frequency_table_get_index(policy, policy->cur); 1182d3916691SViresh Kumar if (ret == -EINVAL) { 1183d3916691SViresh Kumar /* Warn user and fix it */ 1184d3916691SViresh Kumar pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n", 1185d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1186d3916691SViresh Kumar ret = __cpufreq_driver_target(policy, policy->cur - 1, 1187d3916691SViresh Kumar CPUFREQ_RELATION_L); 1188d3916691SViresh Kumar 1189d3916691SViresh Kumar /* 1190d3916691SViresh Kumar * Reaching here after boot in a few seconds may not 1191d3916691SViresh Kumar * mean that system will remain stable at "unknown" 1192d3916691SViresh Kumar * frequency for longer duration. Hence, a BUG_ON(). 1193d3916691SViresh Kumar */ 1194d3916691SViresh Kumar BUG_ON(ret); 1195d3916691SViresh Kumar pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n", 1196d3916691SViresh Kumar __func__, policy->cpu, policy->cur); 1197d3916691SViresh Kumar } 1198d3916691SViresh Kumar } 1199d3916691SViresh Kumar 1200a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1201a1531acdSThomas Renninger CPUFREQ_START, policy); 1202a1531acdSThomas Renninger 1203194d99c7SRafael J. Wysocki if (new_policy) { 1204d9612a49SRafael J. Wysocki ret = cpufreq_add_dev_interface(policy); 120519d6f7ecSDave Jones if (ret) 12068101f997SViresh Kumar goto out_exit_policy; 1207fcd7af91SViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1208fcd7af91SViresh Kumar CPUFREQ_CREATE_POLICY, policy); 1209c88a1f8bSLukasz Majewski 1210c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1211c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1212c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1213988bed09SViresh Kumar } 12148ff69732SDave Jones 12157f0fa40fSViresh Kumar ret = cpufreq_init_policy(policy); 12167f0fa40fSViresh Kumar if (ret) { 12177f0fa40fSViresh Kumar pr_err("%s: Failed to initialize policy for cpu: %d (%d)\n", 12187f0fa40fSViresh Kumar __func__, cpu, ret); 1219194d99c7SRafael J. Wysocki /* cpufreq_policy_free() will notify based on this */ 1220194d99c7SRafael J. Wysocki new_policy = false; 1221194d99c7SRafael J. Wysocki goto out_exit_policy; 122208fd8c1cSViresh Kumar } 1223e18f1682SSrivatsa S. Bhat 12244e97b631SViresh Kumar up_write(&policy->rwsem); 122508fd8c1cSViresh Kumar 1226038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 12277c45cf31SViresh Kumar 12287c45cf31SViresh Kumar /* Callback for handling stuff after policy is ready */ 12297c45cf31SViresh Kumar if (cpufreq_driver->ready) 12307c45cf31SViresh Kumar cpufreq_driver->ready(policy); 12317c45cf31SViresh Kumar 12322d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 12331da177e4SLinus Torvalds 12341da177e4SLinus Torvalds return 0; 12351da177e4SLinus Torvalds 12368101f997SViresh Kumar out_exit_policy: 12377106e02bSPrarit Bhargava up_write(&policy->rwsem); 12387106e02bSPrarit Bhargava 1239da60ce9fSViresh Kumar if (cpufreq_driver->exit) 1240da60ce9fSViresh Kumar cpufreq_driver->exit(policy); 12418101f997SViresh Kumar out_free_policy: 1242194d99c7SRafael J. Wysocki cpufreq_policy_free(policy, !new_policy); 12431da177e4SLinus Torvalds return ret; 12441da177e4SLinus Torvalds } 12451da177e4SLinus Torvalds 12460b275352SRafael J. Wysocki /** 12470b275352SRafael J. Wysocki * cpufreq_add_dev - the cpufreq interface for a CPU device. 12480b275352SRafael J. Wysocki * @dev: CPU device. 12490b275352SRafael J. Wysocki * @sif: Subsystem interface structure pointer (not used) 12500b275352SRafael J. Wysocki */ 12510b275352SRafael J. Wysocki static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 12520b275352SRafael J. Wysocki { 12530b275352SRafael J. Wysocki unsigned cpu = dev->id; 12540b275352SRafael J. Wysocki int ret; 12550b275352SRafael J. Wysocki 12560b275352SRafael J. Wysocki dev_dbg(dev, "%s: adding CPU%u\n", __func__, cpu); 12570b275352SRafael J. Wysocki 12580b275352SRafael J. Wysocki if (cpu_online(cpu)) { 12590b275352SRafael J. Wysocki ret = cpufreq_online(cpu); 12600b275352SRafael J. Wysocki } else { 12610b275352SRafael J. Wysocki /* 12620b275352SRafael J. Wysocki * A hotplug notifier will follow and we will handle it as CPU 12630b275352SRafael J. Wysocki * online then. For now, just create the sysfs link, unless 12640b275352SRafael J. Wysocki * there is no policy or the link is already present. 12650b275352SRafael J. Wysocki */ 12660b275352SRafael J. Wysocki struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 12670b275352SRafael J. Wysocki 12680b275352SRafael J. Wysocki ret = policy && !cpumask_test_and_set_cpu(cpu, policy->real_cpus) 12690b275352SRafael J. Wysocki ? add_cpu_dev_symlink(policy, cpu) : 0; 12700b275352SRafael J. Wysocki } 12711da177e4SLinus Torvalds 12721da177e4SLinus Torvalds return ret; 12731da177e4SLinus Torvalds } 12741da177e4SLinus Torvalds 127569cee714SViresh Kumar static void cpufreq_offline(unsigned int cpu) 12761da177e4SLinus Torvalds { 12773a3e9e06SViresh Kumar struct cpufreq_policy *policy; 127869cee714SViresh Kumar int ret; 12791da177e4SLinus Torvalds 1280b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 12811da177e4SLinus Torvalds 1282988bed09SViresh Kumar policy = cpufreq_cpu_get_raw(cpu); 12833a3e9e06SViresh Kumar if (!policy) { 1284b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 128515c0b4d2SRafael J. Wysocki return; 12861da177e4SLinus Torvalds } 12871da177e4SLinus Torvalds 128849f18560SViresh Kumar down_write(&policy->rwsem); 12899c0ebcf7SViresh Kumar if (has_target()) { 1290a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 1291559ed407SRafael J. Wysocki if (ret) 12923de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 1293db5f2995SViresh Kumar } 12941da177e4SLinus Torvalds 12959591becbSViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 12964573237bSViresh Kumar 12979591becbSViresh Kumar if (policy_is_inactive(policy)) { 12989591becbSViresh Kumar if (has_target()) 12994573237bSViresh Kumar strncpy(policy->last_governor, policy->governor->name, 13004573237bSViresh Kumar CPUFREQ_NAME_LEN); 130169030dd1SSrinivas Pandruvada else 130269030dd1SSrinivas Pandruvada policy->last_policy = policy->policy; 13039591becbSViresh Kumar } else if (cpu == policy->cpu) { 13049591becbSViresh Kumar /* Nominate new CPU */ 13059591becbSViresh Kumar policy->cpu = cpumask_any(policy->cpus); 13069591becbSViresh Kumar } 13071da177e4SLinus Torvalds 13089591becbSViresh Kumar /* Start governor again for active policy */ 13099591becbSViresh Kumar if (!policy_is_inactive(policy)) { 13109591becbSViresh Kumar if (has_target()) { 1311a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 13129591becbSViresh Kumar if (!ret) 1313a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 131487549141SViresh Kumar 13159591becbSViresh Kumar if (ret) 13169591becbSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 13179591becbSViresh Kumar } 131869cee714SViresh Kumar 131949f18560SViresh Kumar goto unlock; 132069cee714SViresh Kumar } 132169cee714SViresh Kumar 132269cee714SViresh Kumar if (cpufreq_driver->stop_cpu) 1323367dc4aaSDirk Brandewie cpufreq_driver->stop_cpu(policy); 132487549141SViresh Kumar 132587549141SViresh Kumar /* If cpu is last user of policy, free policy */ 132687549141SViresh Kumar if (has_target()) { 1327a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 1328559ed407SRafael J. Wysocki if (ret) 132987549141SViresh Kumar pr_err("%s: Failed to exit governor\n", __func__); 13303de9bdebSViresh Kumar } 13312a998599SRafael J. Wysocki 13328414809cSSrivatsa S. Bhat /* 13338414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 13348414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 13358414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 13368414809cSSrivatsa S. Bhat */ 133755582bccSSrinivas Pandruvada if (cpufreq_driver->exit) { 13383a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 133955582bccSSrinivas Pandruvada policy->freq_table = NULL; 134055582bccSSrinivas Pandruvada } 134149f18560SViresh Kumar 134249f18560SViresh Kumar unlock: 134349f18560SViresh Kumar up_write(&policy->rwsem); 13441da177e4SLinus Torvalds } 13451da177e4SLinus Torvalds 1346cedb70afSSrivatsa S. Bhat /** 134727a862e9SViresh Kumar * cpufreq_remove_dev - remove a CPU device 1348cedb70afSSrivatsa S. Bhat * 1349cedb70afSSrivatsa S. Bhat * Removes the cpufreq interface for a CPU device. 1350cedb70afSSrivatsa S. Bhat */ 135171db87baSViresh Kumar static void cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 13525a01f2e8SVenkatesh Pallipadi { 13538a25a2fdSKay Sievers unsigned int cpu = dev->id; 135487549141SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 135587549141SViresh Kumar 135687549141SViresh Kumar if (!policy) 135771db87baSViresh Kumar return; 1358ec28297aSVenki Pallipadi 135969cee714SViresh Kumar if (cpu_online(cpu)) 136069cee714SViresh Kumar cpufreq_offline(cpu); 136187549141SViresh Kumar 1362559ed407SRafael J. Wysocki cpumask_clear_cpu(cpu, policy->real_cpus); 1363559ed407SRafael J. Wysocki remove_cpu_dev_symlink(policy, cpu); 1364f344dae0SViresh Kumar 1365f344dae0SViresh Kumar if (cpumask_empty(policy->real_cpus)) 1366f344dae0SViresh Kumar cpufreq_policy_free(policy, true); 13675a01f2e8SVenkatesh Pallipadi } 13685a01f2e8SVenkatesh Pallipadi 13691da177e4SLinus Torvalds /** 1370bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1371bb176f7dSViresh Kumar * in deep trouble. 1372a1e1dc41SViresh Kumar * @policy: policy managing CPUs 13731da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 13741da177e4SLinus Torvalds * 137529464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 137629464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 13771da177e4SLinus Torvalds */ 1378a1e1dc41SViresh Kumar static void cpufreq_out_of_sync(struct cpufreq_policy *policy, 1379e08f5f5bSGautham R Shenoy unsigned int new_freq) 13801da177e4SLinus Torvalds { 13811da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1382b43a7ffbSViresh Kumar 1383e837f9b5SJoe Perches pr_debug("Warning: CPU frequency out of sync: cpufreq and timing core thinks of %u, is %u kHz\n", 1384a1e1dc41SViresh Kumar policy->cur, new_freq); 13851da177e4SLinus Torvalds 1386a1e1dc41SViresh Kumar freqs.old = policy->cur; 13871da177e4SLinus Torvalds freqs.new = new_freq; 1388b43a7ffbSViresh Kumar 13898fec051eSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 13908fec051eSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 13931da177e4SLinus Torvalds /** 13944ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 139595235ca2SVenkatesh Pallipadi * @cpu: CPU number 139695235ca2SVenkatesh Pallipadi * 139795235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 139895235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 139995235ca2SVenkatesh Pallipadi */ 140095235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 140195235ca2SVenkatesh Pallipadi { 14029e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1403e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 1404*c75361c0SRichard Cochran unsigned long flags; 140595235ca2SVenkatesh Pallipadi 1406*c75361c0SRichard Cochran read_lock_irqsave(&cpufreq_driver_lock, flags); 1407*c75361c0SRichard Cochran 1408*c75361c0SRichard Cochran if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) { 1409*c75361c0SRichard Cochran ret_freq = cpufreq_driver->get(cpu); 1410*c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1411*c75361c0SRichard Cochran return ret_freq; 1412*c75361c0SRichard Cochran } 1413*c75361c0SRichard Cochran 1414*c75361c0SRichard Cochran read_unlock_irqrestore(&cpufreq_driver_lock, flags); 14159e21ba8bSDirk Brandewie 14169e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 141795235ca2SVenkatesh Pallipadi if (policy) { 1418e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 141995235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 142095235ca2SVenkatesh Pallipadi } 142195235ca2SVenkatesh Pallipadi 14224d34a67dSDave Jones return ret_freq; 142395235ca2SVenkatesh Pallipadi } 142495235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 142595235ca2SVenkatesh Pallipadi 14263d737108SJesse Barnes /** 14273d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 14283d737108SJesse Barnes * @cpu: CPU number 14293d737108SJesse Barnes * 14303d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 14313d737108SJesse Barnes */ 14323d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 14333d737108SJesse Barnes { 14343d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 14353d737108SJesse Barnes unsigned int ret_freq = 0; 14363d737108SJesse Barnes 14373d737108SJesse Barnes if (policy) { 14383d737108SJesse Barnes ret_freq = policy->max; 14393d737108SJesse Barnes cpufreq_cpu_put(policy); 14403d737108SJesse Barnes } 14413d737108SJesse Barnes 14423d737108SJesse Barnes return ret_freq; 14433d737108SJesse Barnes } 14443d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 14453d737108SJesse Barnes 1446d92d50a4SViresh Kumar static unsigned int __cpufreq_get(struct cpufreq_policy *policy) 14471da177e4SLinus Torvalds { 1448e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 14491da177e4SLinus Torvalds 14501c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 14514d34a67dSDave Jones return ret_freq; 14521da177e4SLinus Torvalds 1453d92d50a4SViresh Kumar ret_freq = cpufreq_driver->get(policy->cpu); 14541da177e4SLinus Torvalds 145511e584cfSViresh Kumar /* Updating inactive policies is invalid, so avoid doing that. */ 145611e584cfSViresh Kumar if (unlikely(policy_is_inactive(policy))) 145711e584cfSViresh Kumar return ret_freq; 145811e584cfSViresh Kumar 1459e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 14601c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1461e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1462e08f5f5bSGautham R Shenoy saved value exists */ 1463e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1464a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, ret_freq); 14651da177e4SLinus Torvalds schedule_work(&policy->update); 14661da177e4SLinus Torvalds } 14671da177e4SLinus Torvalds } 14681da177e4SLinus Torvalds 14694d34a67dSDave Jones return ret_freq; 14705a01f2e8SVenkatesh Pallipadi } 14711da177e4SLinus Torvalds 14725a01f2e8SVenkatesh Pallipadi /** 14735a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 14745a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 14755a01f2e8SVenkatesh Pallipadi * 14765a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 14775a01f2e8SVenkatesh Pallipadi */ 14785a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 14795a01f2e8SVenkatesh Pallipadi { 1480999976e0SAaron Plattner struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 14815a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 14825a01f2e8SVenkatesh Pallipadi 1483999976e0SAaron Plattner if (policy) { 1484ad7722daSviresh kumar down_read(&policy->rwsem); 1485d92d50a4SViresh Kumar ret_freq = __cpufreq_get(policy); 1486ad7722daSviresh kumar up_read(&policy->rwsem); 1487999976e0SAaron Plattner 1488999976e0SAaron Plattner cpufreq_cpu_put(policy); 1489999976e0SAaron Plattner } 14906eed9404SViresh Kumar 14914d34a67dSDave Jones return ret_freq; 14921da177e4SLinus Torvalds } 14931da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 14941da177e4SLinus Torvalds 14958a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 14968a25a2fdSKay Sievers .name = "cpufreq", 14978a25a2fdSKay Sievers .subsys = &cpu_subsys, 14988a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 14998a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1500e00e56dfSRafael J. Wysocki }; 1501e00e56dfSRafael J. Wysocki 1502e28867eaSViresh Kumar /* 1503e28867eaSViresh Kumar * In case platform wants some specific frequency to be configured 1504e28867eaSViresh Kumar * during suspend.. 150542d4dc3fSBenjamin Herrenschmidt */ 1506e28867eaSViresh Kumar int cpufreq_generic_suspend(struct cpufreq_policy *policy) 150742d4dc3fSBenjamin Herrenschmidt { 1508e28867eaSViresh Kumar int ret; 15094bc5d341SDave Jones 1510e28867eaSViresh Kumar if (!policy->suspend_freq) { 1511201f3716SBartlomiej Zolnierkiewicz pr_debug("%s: suspend_freq not defined\n", __func__); 1512201f3716SBartlomiej Zolnierkiewicz return 0; 151342d4dc3fSBenjamin Herrenschmidt } 151442d4dc3fSBenjamin Herrenschmidt 1515e28867eaSViresh Kumar pr_debug("%s: Setting suspend-freq: %u\n", __func__, 1516e28867eaSViresh Kumar policy->suspend_freq); 1517e28867eaSViresh Kumar 1518e28867eaSViresh Kumar ret = __cpufreq_driver_target(policy, policy->suspend_freq, 1519e28867eaSViresh Kumar CPUFREQ_RELATION_H); 1520e28867eaSViresh Kumar if (ret) 1521e28867eaSViresh Kumar pr_err("%s: unable to set suspend-freq: %u. err: %d\n", 1522e28867eaSViresh Kumar __func__, policy->suspend_freq, ret); 1523e28867eaSViresh Kumar 1524c9060494SDave Jones return ret; 152542d4dc3fSBenjamin Herrenschmidt } 1526e28867eaSViresh Kumar EXPORT_SYMBOL(cpufreq_generic_suspend); 152742d4dc3fSBenjamin Herrenschmidt 152842d4dc3fSBenjamin Herrenschmidt /** 15292f0aea93SViresh Kumar * cpufreq_suspend() - Suspend CPUFreq governors 15301da177e4SLinus Torvalds * 15312f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycles for suspending governors 15322f0aea93SViresh Kumar * as some platforms can't change frequency after this point in suspend cycle. 15332f0aea93SViresh Kumar * Because some of the devices (like: i2c, regulators, etc) they use for 15342f0aea93SViresh Kumar * changing frequency are suspended quickly after this point. 15351da177e4SLinus Torvalds */ 15362f0aea93SViresh Kumar void cpufreq_suspend(void) 15371da177e4SLinus Torvalds { 15383a3e9e06SViresh Kumar struct cpufreq_policy *policy; 153949f18560SViresh Kumar int ret; 15401da177e4SLinus Torvalds 15412f0aea93SViresh Kumar if (!cpufreq_driver) 1542e00e56dfSRafael J. Wysocki return; 15431da177e4SLinus Torvalds 15442f0aea93SViresh Kumar if (!has_target()) 1545b1b12babSViresh Kumar goto suspend; 15461da177e4SLinus Torvalds 15472f0aea93SViresh Kumar pr_debug("%s: Suspending Governors\n", __func__); 15482f0aea93SViresh Kumar 1549f963735aSViresh Kumar for_each_active_policy(policy) { 155049f18560SViresh Kumar down_write(&policy->rwsem); 1551a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 155249f18560SViresh Kumar up_write(&policy->rwsem); 155349f18560SViresh Kumar 155449f18560SViresh Kumar if (ret) 15552f0aea93SViresh Kumar pr_err("%s: Failed to stop governor for policy: %p\n", 15562f0aea93SViresh Kumar __func__, policy); 15572f0aea93SViresh Kumar else if (cpufreq_driver->suspend 15582f0aea93SViresh Kumar && cpufreq_driver->suspend(policy)) 15592f0aea93SViresh Kumar pr_err("%s: Failed to suspend driver: %p\n", __func__, 15602f0aea93SViresh Kumar policy); 15611da177e4SLinus Torvalds } 1562b1b12babSViresh Kumar 1563b1b12babSViresh Kumar suspend: 1564b1b12babSViresh Kumar cpufreq_suspended = true; 15651da177e4SLinus Torvalds } 15661da177e4SLinus Torvalds 15671da177e4SLinus Torvalds /** 15682f0aea93SViresh Kumar * cpufreq_resume() - Resume CPUFreq governors 15691da177e4SLinus Torvalds * 15702f0aea93SViresh Kumar * Called during system wide Suspend/Hibernate cycle for resuming governors that 15712f0aea93SViresh Kumar * are suspended with cpufreq_suspend(). 15721da177e4SLinus Torvalds */ 15732f0aea93SViresh Kumar void cpufreq_resume(void) 15741da177e4SLinus Torvalds { 15751da177e4SLinus Torvalds struct cpufreq_policy *policy; 157649f18560SViresh Kumar int ret; 15771da177e4SLinus Torvalds 15782f0aea93SViresh Kumar if (!cpufreq_driver) 15791da177e4SLinus Torvalds return; 15801da177e4SLinus Torvalds 15818e30444eSLan Tianyu cpufreq_suspended = false; 15828e30444eSLan Tianyu 15832f0aea93SViresh Kumar if (!has_target()) 15842f0aea93SViresh Kumar return; 15851da177e4SLinus Torvalds 15862f0aea93SViresh Kumar pr_debug("%s: Resuming Governors\n", __func__); 15872f0aea93SViresh Kumar 1588f963735aSViresh Kumar for_each_active_policy(policy) { 158949f18560SViresh Kumar if (cpufreq_driver->resume && cpufreq_driver->resume(policy)) { 15900c5aa405SViresh Kumar pr_err("%s: Failed to resume driver: %p\n", __func__, 15910c5aa405SViresh Kumar policy); 159249f18560SViresh Kumar } else { 159349f18560SViresh Kumar down_write(&policy->rwsem); 1594a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 159549f18560SViresh Kumar if (!ret) 1596a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 159749f18560SViresh Kumar up_write(&policy->rwsem); 159849f18560SViresh Kumar 159949f18560SViresh Kumar if (ret) 16002f0aea93SViresh Kumar pr_err("%s: Failed to start governor for policy: %p\n", 16012f0aea93SViresh Kumar __func__, policy); 1602c75de0acSViresh Kumar } 160349f18560SViresh Kumar } 16042f0aea93SViresh Kumar 16052f0aea93SViresh Kumar /* 1606c75de0acSViresh Kumar * schedule call cpufreq_update_policy() for first-online CPU, as that 1607c75de0acSViresh Kumar * wouldn't be hotplugged-out on suspend. It will verify that the 1608c75de0acSViresh Kumar * current freq is in sync with what we believe it to be. 16092f0aea93SViresh Kumar */ 1610c75de0acSViresh Kumar policy = cpufreq_cpu_get_raw(cpumask_first(cpu_online_mask)); 1611c75de0acSViresh Kumar if (WARN_ON(!policy)) 1612c75de0acSViresh Kumar return; 1613c75de0acSViresh Kumar 16143a3e9e06SViresh Kumar schedule_work(&policy->update); 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16179d95046eSBorislav Petkov /** 16189d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 16199d95046eSBorislav Petkov * 16209d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 16219d95046eSBorislav Petkov * or NULL, if none. 16229d95046eSBorislav Petkov */ 16239d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 16249d95046eSBorislav Petkov { 16251c3d85ddSRafael J. Wysocki if (cpufreq_driver) 16261c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 16271c3d85ddSRafael J. Wysocki 16281c3d85ddSRafael J. Wysocki return NULL; 16299d95046eSBorislav Petkov } 16309d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 16311da177e4SLinus Torvalds 163251315cdfSThomas Petazzoni /** 163351315cdfSThomas Petazzoni * cpufreq_get_driver_data - return current driver data 163451315cdfSThomas Petazzoni * 163551315cdfSThomas Petazzoni * Return the private data of the currently loaded cpufreq 163651315cdfSThomas Petazzoni * driver, or NULL if no cpufreq driver is loaded. 163751315cdfSThomas Petazzoni */ 163851315cdfSThomas Petazzoni void *cpufreq_get_driver_data(void) 163951315cdfSThomas Petazzoni { 164051315cdfSThomas Petazzoni if (cpufreq_driver) 164151315cdfSThomas Petazzoni return cpufreq_driver->driver_data; 164251315cdfSThomas Petazzoni 164351315cdfSThomas Petazzoni return NULL; 164451315cdfSThomas Petazzoni } 164551315cdfSThomas Petazzoni EXPORT_SYMBOL_GPL(cpufreq_get_driver_data); 164651315cdfSThomas Petazzoni 16471da177e4SLinus Torvalds /********************************************************************* 16481da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 16491da177e4SLinus Torvalds *********************************************************************/ 16501da177e4SLinus Torvalds 16511da177e4SLinus Torvalds /** 16521da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 16531da177e4SLinus Torvalds * @nb: notifier function to register 16541da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 16551da177e4SLinus Torvalds * 16561da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 16571da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 16581da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 16591da177e4SLinus Torvalds * changes in cpufreq policy. 16601da177e4SLinus Torvalds * 16611da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1662e041c683SAlan Stern * blocking_notifier_chain_register. 16631da177e4SLinus Torvalds */ 16641da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 16651da177e4SLinus Torvalds { 16661da177e4SLinus Torvalds int ret; 16671da177e4SLinus Torvalds 1668d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1669d5aaffa9SDirk Brandewie return -EINVAL; 1670d5aaffa9SDirk Brandewie 167174212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 167274212ca4SCesar Eduardo Barros 16731da177e4SLinus Torvalds switch (list) { 16741da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1675b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1676e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 16771da177e4SLinus Torvalds break; 16781da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1679e041c683SAlan Stern ret = blocking_notifier_chain_register( 1680e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 16811da177e4SLinus Torvalds break; 16821da177e4SLinus Torvalds default: 16831da177e4SLinus Torvalds ret = -EINVAL; 16841da177e4SLinus Torvalds } 16851da177e4SLinus Torvalds 16861da177e4SLinus Torvalds return ret; 16871da177e4SLinus Torvalds } 16881da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 16891da177e4SLinus Torvalds 16901da177e4SLinus Torvalds /** 16911da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 16921da177e4SLinus Torvalds * @nb: notifier block to be unregistered 16931da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 16941da177e4SLinus Torvalds * 16951da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 16961da177e4SLinus Torvalds * 16971da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1698e041c683SAlan Stern * blocking_notifier_chain_unregister. 16991da177e4SLinus Torvalds */ 17001da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 17011da177e4SLinus Torvalds { 17021da177e4SLinus Torvalds int ret; 17031da177e4SLinus Torvalds 1704d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1705d5aaffa9SDirk Brandewie return -EINVAL; 1706d5aaffa9SDirk Brandewie 17071da177e4SLinus Torvalds switch (list) { 17081da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1709b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1710e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 17111da177e4SLinus Torvalds break; 17121da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1713e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1714e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 17151da177e4SLinus Torvalds break; 17161da177e4SLinus Torvalds default: 17171da177e4SLinus Torvalds ret = -EINVAL; 17181da177e4SLinus Torvalds } 17191da177e4SLinus Torvalds 17201da177e4SLinus Torvalds return ret; 17211da177e4SLinus Torvalds } 17221da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 17231da177e4SLinus Torvalds 17241da177e4SLinus Torvalds 17251da177e4SLinus Torvalds /********************************************************************* 17261da177e4SLinus Torvalds * GOVERNORS * 17271da177e4SLinus Torvalds *********************************************************************/ 17281da177e4SLinus Torvalds 17291c03a2d0SViresh Kumar /* Must set freqs->new to intermediate frequency */ 17301c03a2d0SViresh Kumar static int __target_intermediate(struct cpufreq_policy *policy, 17311c03a2d0SViresh Kumar struct cpufreq_freqs *freqs, int index) 17321c03a2d0SViresh Kumar { 17331c03a2d0SViresh Kumar int ret; 17341c03a2d0SViresh Kumar 17351c03a2d0SViresh Kumar freqs->new = cpufreq_driver->get_intermediate(policy, index); 17361c03a2d0SViresh Kumar 17371c03a2d0SViresh Kumar /* We don't need to switch to intermediate freq */ 17381c03a2d0SViresh Kumar if (!freqs->new) 17391c03a2d0SViresh Kumar return 0; 17401c03a2d0SViresh Kumar 17411c03a2d0SViresh Kumar pr_debug("%s: cpu: %d, switching to intermediate freq: oldfreq: %u, intermediate freq: %u\n", 17421c03a2d0SViresh Kumar __func__, policy->cpu, freqs->old, freqs->new); 17431c03a2d0SViresh Kumar 17441c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, freqs); 17451c03a2d0SViresh Kumar ret = cpufreq_driver->target_intermediate(policy, index); 17461c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, freqs, ret); 17471c03a2d0SViresh Kumar 17481c03a2d0SViresh Kumar if (ret) 17491c03a2d0SViresh Kumar pr_err("%s: Failed to change to intermediate frequency: %d\n", 17501c03a2d0SViresh Kumar __func__, ret); 17511c03a2d0SViresh Kumar 17521c03a2d0SViresh Kumar return ret; 17531c03a2d0SViresh Kumar } 17541c03a2d0SViresh Kumar 17558d65775dSViresh Kumar static int __target_index(struct cpufreq_policy *policy, 17568d65775dSViresh Kumar struct cpufreq_frequency_table *freq_table, int index) 17578d65775dSViresh Kumar { 17581c03a2d0SViresh Kumar struct cpufreq_freqs freqs = {.old = policy->cur, .flags = 0}; 17591c03a2d0SViresh Kumar unsigned int intermediate_freq = 0; 17608d65775dSViresh Kumar int retval = -EINVAL; 17618d65775dSViresh Kumar bool notify; 17628d65775dSViresh Kumar 17638d65775dSViresh Kumar notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION); 17648d65775dSViresh Kumar if (notify) { 17651c03a2d0SViresh Kumar /* Handle switching to intermediate frequency */ 17661c03a2d0SViresh Kumar if (cpufreq_driver->get_intermediate) { 17671c03a2d0SViresh Kumar retval = __target_intermediate(policy, &freqs, index); 17681c03a2d0SViresh Kumar if (retval) 17691c03a2d0SViresh Kumar return retval; 17708d65775dSViresh Kumar 17711c03a2d0SViresh Kumar intermediate_freq = freqs.new; 17721c03a2d0SViresh Kumar /* Set old freq to intermediate */ 17731c03a2d0SViresh Kumar if (intermediate_freq) 17741c03a2d0SViresh Kumar freqs.old = freqs.new; 17751c03a2d0SViresh Kumar } 17761c03a2d0SViresh Kumar 17771c03a2d0SViresh Kumar freqs.new = freq_table[index].frequency; 17788d65775dSViresh Kumar pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n", 17798d65775dSViresh Kumar __func__, policy->cpu, freqs.old, freqs.new); 17808d65775dSViresh Kumar 17818d65775dSViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 17828d65775dSViresh Kumar } 17838d65775dSViresh Kumar 17848d65775dSViresh Kumar retval = cpufreq_driver->target_index(policy, index); 17858d65775dSViresh Kumar if (retval) 17868d65775dSViresh Kumar pr_err("%s: Failed to change cpu frequency: %d\n", __func__, 17878d65775dSViresh Kumar retval); 17888d65775dSViresh Kumar 17891c03a2d0SViresh Kumar if (notify) { 17908d65775dSViresh Kumar cpufreq_freq_transition_end(policy, &freqs, retval); 17918d65775dSViresh Kumar 17921c03a2d0SViresh Kumar /* 17931c03a2d0SViresh Kumar * Failed after setting to intermediate freq? Driver should have 17941c03a2d0SViresh Kumar * reverted back to initial frequency and so should we. Check 17951c03a2d0SViresh Kumar * here for intermediate_freq instead of get_intermediate, in 179658405af6SShailendra Verma * case we haven't switched to intermediate freq at all. 17971c03a2d0SViresh Kumar */ 17981c03a2d0SViresh Kumar if (unlikely(retval && intermediate_freq)) { 17991c03a2d0SViresh Kumar freqs.old = intermediate_freq; 18001c03a2d0SViresh Kumar freqs.new = policy->restore_freq; 18011c03a2d0SViresh Kumar cpufreq_freq_transition_begin(policy, &freqs); 18021c03a2d0SViresh Kumar cpufreq_freq_transition_end(policy, &freqs, 0); 18031c03a2d0SViresh Kumar } 18041c03a2d0SViresh Kumar } 18051c03a2d0SViresh Kumar 18068d65775dSViresh Kumar return retval; 18078d65775dSViresh Kumar } 18088d65775dSViresh Kumar 18091da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 18101da177e4SLinus Torvalds unsigned int target_freq, 18111da177e4SLinus Torvalds unsigned int relation) 18121da177e4SLinus Torvalds { 18137249924eSViresh Kumar unsigned int old_target_freq = target_freq; 18146019d23aSRafael J. Wysocki struct cpufreq_frequency_table *freq_table; 18156019d23aSRafael J. Wysocki int index, retval; 1816c32b6b8eSAshok Raj 1817a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1818a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1819a7b422cdSKonrad Rzeszutek Wilk 18207249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 18217249924eSViresh Kumar if (target_freq > policy->max) 18227249924eSViresh Kumar target_freq = policy->max; 18237249924eSViresh Kumar if (target_freq < policy->min) 18247249924eSViresh Kumar target_freq = policy->min; 18257249924eSViresh Kumar 18267249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 18277249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 18285a1c0228SViresh Kumar 18299c0ebcf7SViresh Kumar /* 18309c0ebcf7SViresh Kumar * This might look like a redundant call as we are checking it again 18319c0ebcf7SViresh Kumar * after finding index. But it is left intentionally for cases where 18329c0ebcf7SViresh Kumar * exactly same freq is called again and so we can save on few function 18339c0ebcf7SViresh Kumar * calls. 18349c0ebcf7SViresh Kumar */ 18355a1c0228SViresh Kumar if (target_freq == policy->cur) 18365a1c0228SViresh Kumar return 0; 18375a1c0228SViresh Kumar 18381c03a2d0SViresh Kumar /* Save last value to restore later on errors */ 18391c03a2d0SViresh Kumar policy->restore_freq = policy->cur; 18401c03a2d0SViresh Kumar 18411c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 18426019d23aSRafael J. Wysocki return cpufreq_driver->target(policy, target_freq, relation); 18436019d23aSRafael J. Wysocki 18446019d23aSRafael J. Wysocki if (!cpufreq_driver->target_index) 18456019d23aSRafael J. Wysocki return -EINVAL; 184690d45d17SAshok Raj 18479c0ebcf7SViresh Kumar freq_table = cpufreq_frequency_get_table(policy->cpu); 18489c0ebcf7SViresh Kumar if (unlikely(!freq_table)) { 18499c0ebcf7SViresh Kumar pr_err("%s: Unable to find freq_table\n", __func__); 18506019d23aSRafael J. Wysocki return -EINVAL; 18519c0ebcf7SViresh Kumar } 18529c0ebcf7SViresh Kumar 18536019d23aSRafael J. Wysocki retval = cpufreq_frequency_table_target(policy, freq_table, target_freq, 18546019d23aSRafael J. Wysocki relation, &index); 18559c0ebcf7SViresh Kumar if (unlikely(retval)) { 18569c0ebcf7SViresh Kumar pr_err("%s: Unable to find matching freq\n", __func__); 18571da177e4SLinus Torvalds return retval; 18581da177e4SLinus Torvalds } 18596019d23aSRafael J. Wysocki 18606019d23aSRafael J. Wysocki if (freq_table[index].frequency == policy->cur) 18616019d23aSRafael J. Wysocki return 0; 18626019d23aSRafael J. Wysocki 18636019d23aSRafael J. Wysocki return __target_index(policy, freq_table, index); 18646019d23aSRafael J. Wysocki } 18651da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 18661da177e4SLinus Torvalds 18671da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 18681da177e4SLinus Torvalds unsigned int target_freq, 18691da177e4SLinus Torvalds unsigned int relation) 18701da177e4SLinus Torvalds { 1871f1829e4aSJulia Lawall int ret = -EINVAL; 18721da177e4SLinus Torvalds 1873ad7722daSviresh kumar down_write(&policy->rwsem); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 18761da177e4SLinus Torvalds 1877ad7722daSviresh kumar up_write(&policy->rwsem); 18781da177e4SLinus Torvalds 18791da177e4SLinus Torvalds return ret; 18801da177e4SLinus Torvalds } 18811da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 18821da177e4SLinus Torvalds 1883de1df26bSRafael J. Wysocki __weak struct cpufreq_governor *cpufreq_fallback_governor(void) 1884de1df26bSRafael J. Wysocki { 1885de1df26bSRafael J. Wysocki return NULL; 1886de1df26bSRafael J. Wysocki } 1887de1df26bSRafael J. Wysocki 1888a1317e09SViresh Kumar static int cpufreq_governor(struct cpufreq_policy *policy, unsigned int event) 18891da177e4SLinus Torvalds { 1890cc993cabSDave Jones int ret; 18916afde10cSThomas Renninger 18922f0aea93SViresh Kumar /* Don't start any governor operations if we are entering suspend */ 18932f0aea93SViresh Kumar if (cpufreq_suspended) 18942f0aea93SViresh Kumar return 0; 1895cb57720bSEthan Zhao /* 1896cb57720bSEthan Zhao * Governor might not be initiated here if ACPI _PPC changed 1897cb57720bSEthan Zhao * notification happened, so check it. 1898cb57720bSEthan Zhao */ 1899cb57720bSEthan Zhao if (!policy->governor) 1900cb57720bSEthan Zhao return -EINVAL; 19012f0aea93SViresh Kumar 19021c256245SThomas Renninger if (policy->governor->max_transition_latency && 19031c256245SThomas Renninger policy->cpuinfo.transition_latency > 19041c256245SThomas Renninger policy->governor->max_transition_latency) { 1905de1df26bSRafael J. Wysocki struct cpufreq_governor *gov = cpufreq_fallback_governor(); 1906de1df26bSRafael J. Wysocki 1907de1df26bSRafael J. Wysocki if (gov) { 1908e837f9b5SJoe Perches pr_warn("%s governor failed, too long transition latency of HW, fallback to %s governor\n", 1909e837f9b5SJoe Perches policy->governor->name, gov->name); 19101c256245SThomas Renninger policy->governor = gov; 1911de1df26bSRafael J. Wysocki } else { 1912de1df26bSRafael J. Wysocki return -EINVAL; 19131c256245SThomas Renninger } 19146afde10cSThomas Renninger } 19151da177e4SLinus Torvalds 1916fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19171da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 19181da177e4SLinus Torvalds return -EINVAL; 19191da177e4SLinus Torvalds 192063431f78SViresh Kumar pr_debug("%s: for CPU %u, event %u\n", __func__, policy->cpu, event); 192195731ebbSXiaoguang Chen 19221da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 19231da177e4SLinus Torvalds 19244d5dcc42SViresh Kumar if (!ret) { 19254d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 19268e53695fSViresh Kumar policy->governor->initialized++; 19274d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 19288e53695fSViresh Kumar policy->governor->initialized--; 19294d5dcc42SViresh Kumar } 1930b394058fSViresh Kumar 1931fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 1932fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 19331da177e4SLinus Torvalds module_put(policy->governor->owner); 19341da177e4SLinus Torvalds 19351da177e4SLinus Torvalds return ret; 19361da177e4SLinus Torvalds } 19371da177e4SLinus Torvalds 19381da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 19391da177e4SLinus Torvalds { 19403bcb09a3SJeremy Fitzhardinge int err; 19411da177e4SLinus Torvalds 19421da177e4SLinus Torvalds if (!governor) 19431da177e4SLinus Torvalds return -EINVAL; 19441da177e4SLinus Torvalds 1945a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1946a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1947a7b422cdSKonrad Rzeszutek Wilk 19483fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 19491da177e4SLinus Torvalds 1950b394058fSViresh Kumar governor->initialized = 0; 19513bcb09a3SJeremy Fitzhardinge err = -EBUSY; 195242f91fa1SViresh Kumar if (!find_governor(governor->name)) { 19533bcb09a3SJeremy Fitzhardinge err = 0; 19541da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 19553bcb09a3SJeremy Fitzhardinge } 19561da177e4SLinus Torvalds 19573fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 19583bcb09a3SJeremy Fitzhardinge return err; 19591da177e4SLinus Torvalds } 19601da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 19611da177e4SLinus Torvalds 19621da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 19631da177e4SLinus Torvalds { 19644573237bSViresh Kumar struct cpufreq_policy *policy; 19654573237bSViresh Kumar unsigned long flags; 196690e41bacSPrarit Bhargava 19671da177e4SLinus Torvalds if (!governor) 19681da177e4SLinus Torvalds return; 19691da177e4SLinus Torvalds 1970a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1971a7b422cdSKonrad Rzeszutek Wilk return; 1972a7b422cdSKonrad Rzeszutek Wilk 19734573237bSViresh Kumar /* clear last_governor for all inactive policies */ 19744573237bSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 19754573237bSViresh Kumar for_each_inactive_policy(policy) { 197618bf3a12SViresh Kumar if (!strcmp(policy->last_governor, governor->name)) { 197718bf3a12SViresh Kumar policy->governor = NULL; 19784573237bSViresh Kumar strcpy(policy->last_governor, "\0"); 197990e41bacSPrarit Bhargava } 198018bf3a12SViresh Kumar } 19814573237bSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 198290e41bacSPrarit Bhargava 19833fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 19841da177e4SLinus Torvalds list_del(&governor->governor_list); 19853fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 19861da177e4SLinus Torvalds return; 19871da177e4SLinus Torvalds } 19881da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 19891da177e4SLinus Torvalds 19901da177e4SLinus Torvalds 19911da177e4SLinus Torvalds /********************************************************************* 19921da177e4SLinus Torvalds * POLICY INTERFACE * 19931da177e4SLinus Torvalds *********************************************************************/ 19941da177e4SLinus Torvalds 19951da177e4SLinus Torvalds /** 19961da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 199729464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 199829464f28SDave Jones * is written 19991da177e4SLinus Torvalds * 20001da177e4SLinus Torvalds * Reads the current cpufreq policy. 20011da177e4SLinus Torvalds */ 20021da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 20031da177e4SLinus Torvalds { 20041da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 20051da177e4SLinus Torvalds if (!policy) 20061da177e4SLinus Torvalds return -EINVAL; 20071da177e4SLinus Torvalds 20081da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 20091da177e4SLinus Torvalds if (!cpu_policy) 20101da177e4SLinus Torvalds return -EINVAL; 20111da177e4SLinus Torvalds 2012d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 20131da177e4SLinus Torvalds 20141da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 20151da177e4SLinus Torvalds return 0; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 20181da177e4SLinus Torvalds 2019153d7f3fSArjan van de Ven /* 2020037ce839SViresh Kumar * policy : current policy. 2021037ce839SViresh Kumar * new_policy: policy to be set. 2022153d7f3fSArjan van de Ven */ 2023037ce839SViresh Kumar static int cpufreq_set_policy(struct cpufreq_policy *policy, 20243a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 20251da177e4SLinus Torvalds { 2026d9a789c7SRafael J. Wysocki struct cpufreq_governor *old_gov; 2027d9a789c7SRafael J. Wysocki int ret; 20281da177e4SLinus Torvalds 2029e837f9b5SJoe Perches pr_debug("setting new policy for CPU %u: %u - %u kHz\n", 2030e837f9b5SJoe Perches new_policy->cpu, new_policy->min, new_policy->max); 20311da177e4SLinus Torvalds 2032d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 20331da177e4SLinus Torvalds 2034fba9573bSPan Xinhui /* 2035fba9573bSPan Xinhui * This check works well when we store new min/max freq attributes, 2036fba9573bSPan Xinhui * because new_policy is a copy of policy with one field updated. 2037fba9573bSPan Xinhui */ 2038fba9573bSPan Xinhui if (new_policy->min > new_policy->max) 2039d9a789c7SRafael J. Wysocki return -EINVAL; 20409c9a43edSMattia Dongili 20411da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 20423a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 20431da177e4SLinus Torvalds if (ret) 2044d9a789c7SRafael J. Wysocki return ret; 20451da177e4SLinus Torvalds 20461da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 2047e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 20483a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 20491da177e4SLinus Torvalds 2050bb176f7dSViresh Kumar /* 2051bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 2052bb176f7dSViresh Kumar * different to the first one 2053bb176f7dSViresh Kumar */ 20543a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 2055e041c683SAlan Stern if (ret) 2056d9a789c7SRafael J. Wysocki return ret; 20571da177e4SLinus Torvalds 20581da177e4SLinus Torvalds /* notification of the new policy */ 2059e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 20603a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 20611da177e4SLinus Torvalds 20623a3e9e06SViresh Kumar policy->min = new_policy->min; 20633a3e9e06SViresh Kumar policy->max = new_policy->max; 20641da177e4SLinus Torvalds 20652d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 20663a3e9e06SViresh Kumar policy->min, policy->max); 20671da177e4SLinus Torvalds 20681c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 20693a3e9e06SViresh Kumar policy->policy = new_policy->policy; 20702d06d8c4SDominik Brodowski pr_debug("setting range\n"); 2071d9a789c7SRafael J. Wysocki return cpufreq_driver->setpolicy(new_policy); 2072d9a789c7SRafael J. Wysocki } 2073d9a789c7SRafael J. Wysocki 2074d9a789c7SRafael J. Wysocki if (new_policy->governor == policy->governor) 2075d9a789c7SRafael J. Wysocki goto out; 20761da177e4SLinus Torvalds 20772d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 20781da177e4SLinus Torvalds 2079d9a789c7SRafael J. Wysocki /* save old, working values */ 2080d9a789c7SRafael J. Wysocki old_gov = policy->governor; 20811da177e4SLinus Torvalds /* end old governor */ 2082d9a789c7SRafael J. Wysocki if (old_gov) { 2083a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_STOP); 20844bc384aeSViresh Kumar if (ret) { 20854bc384aeSViresh Kumar /* This can happen due to race with other operations */ 20864bc384aeSViresh Kumar pr_debug("%s: Failed to Stop Governor: %s (%d)\n", 20874bc384aeSViresh Kumar __func__, old_gov->name, ret); 20884bc384aeSViresh Kumar return ret; 20894bc384aeSViresh Kumar } 20904bc384aeSViresh Kumar 2091a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 20924bc384aeSViresh Kumar if (ret) { 20934bc384aeSViresh Kumar pr_err("%s: Failed to Exit Governor: %s (%d)\n", 20944bc384aeSViresh Kumar __func__, old_gov->name, ret); 20954bc384aeSViresh Kumar return ret; 20964bc384aeSViresh Kumar } 20977bd353a9SViresh Kumar } 20981da177e4SLinus Torvalds 20991da177e4SLinus Torvalds /* start new governor */ 21003a3e9e06SViresh Kumar policy->governor = new_policy->governor; 2101a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT); 21024bc384aeSViresh Kumar if (!ret) { 2103a1317e09SViresh Kumar ret = cpufreq_governor(policy, CPUFREQ_GOV_START); 21044bc384aeSViresh Kumar if (!ret) 2105d9a789c7SRafael J. Wysocki goto out; 2106d9a789c7SRafael J. Wysocki 2107a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_POLICY_EXIT); 2108955ef483SViresh Kumar } 21097bd353a9SViresh Kumar 21101da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 2111d9a789c7SRafael J. Wysocki pr_debug("starting governor %s failed\n", policy->governor->name); 21121da177e4SLinus Torvalds if (old_gov) { 21133a3e9e06SViresh Kumar policy->governor = old_gov; 2114a1317e09SViresh Kumar if (cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) 21154bc384aeSViresh Kumar policy->governor = NULL; 21164bc384aeSViresh Kumar else 2117a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_START); 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds 21204bc384aeSViresh Kumar return ret; 2121d9a789c7SRafael J. Wysocki 2122d9a789c7SRafael J. Wysocki out: 2123d9a789c7SRafael J. Wysocki pr_debug("governor: change or update limits\n"); 2124a1317e09SViresh Kumar return cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 21251da177e4SLinus Torvalds } 21261da177e4SLinus Torvalds 21271da177e4SLinus Torvalds /** 21281da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 21291da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 21301da177e4SLinus Torvalds * 213125985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 21321da177e4SLinus Torvalds * at different times. 21331da177e4SLinus Torvalds */ 21341da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 21351da177e4SLinus Torvalds { 21363a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 21373a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 2138f1829e4aSJulia Lawall int ret; 21391da177e4SLinus Torvalds 2140fefa8ff8SAaron Plattner if (!policy) 2141fefa8ff8SAaron Plattner return -ENODEV; 21421da177e4SLinus Torvalds 2143ad7722daSviresh kumar down_write(&policy->rwsem); 21441da177e4SLinus Torvalds 21452d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 2146d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 21473a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 21483a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 21491da177e4SLinus Torvalds 2150bb176f7dSViresh Kumar /* 2151bb176f7dSViresh Kumar * BIOS might change freq behind our back 2152bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 2153bb176f7dSViresh Kumar */ 21542ed99e39SRafael J. Wysocki if (cpufreq_driver->get && !cpufreq_driver->setpolicy) { 21553a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 2156bd0fa9bbSViresh Kumar if (WARN_ON(!new_policy.cur)) { 2157bd0fa9bbSViresh Kumar ret = -EIO; 2158fefa8ff8SAaron Plattner goto unlock; 2159bd0fa9bbSViresh Kumar } 2160bd0fa9bbSViresh Kumar 21613a3e9e06SViresh Kumar if (!policy->cur) { 2162e837f9b5SJoe Perches pr_debug("Driver did not initialize current freq\n"); 21633a3e9e06SViresh Kumar policy->cur = new_policy.cur; 2164a85f7bd3SThomas Renninger } else { 21659c0ebcf7SViresh Kumar if (policy->cur != new_policy.cur && has_target()) 2166a1e1dc41SViresh Kumar cpufreq_out_of_sync(policy, new_policy.cur); 21670961dd0dSThomas Renninger } 2168a85f7bd3SThomas Renninger } 21690961dd0dSThomas Renninger 2170037ce839SViresh Kumar ret = cpufreq_set_policy(policy, &new_policy); 21711da177e4SLinus Torvalds 2172fefa8ff8SAaron Plattner unlock: 2173ad7722daSviresh kumar up_write(&policy->rwsem); 21745a01f2e8SVenkatesh Pallipadi 21753a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 21761da177e4SLinus Torvalds return ret; 21771da177e4SLinus Torvalds } 21781da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 21791da177e4SLinus Torvalds 21802760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 2181c32b6b8eSAshok Raj unsigned long action, void *hcpu) 2182c32b6b8eSAshok Raj { 2183c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 2184c32b6b8eSAshok Raj 21855302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 2186c32b6b8eSAshok Raj case CPU_ONLINE: 21870b275352SRafael J. Wysocki cpufreq_online(cpu); 2188c32b6b8eSAshok Raj break; 21895302c3fbSSrivatsa S. Bhat 2190c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 219169cee714SViresh Kumar cpufreq_offline(cpu); 2192c32b6b8eSAshok Raj break; 21935302c3fbSSrivatsa S. Bhat 21945a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 21950b275352SRafael J. Wysocki cpufreq_online(cpu); 2196c32b6b8eSAshok Raj break; 2197c32b6b8eSAshok Raj } 2198c32b6b8eSAshok Raj return NOTIFY_OK; 2199c32b6b8eSAshok Raj } 2200c32b6b8eSAshok Raj 22019c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2202c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2203c32b6b8eSAshok Raj }; 22041da177e4SLinus Torvalds 22051da177e4SLinus Torvalds /********************************************************************* 22066f19efc0SLukasz Majewski * BOOST * 22076f19efc0SLukasz Majewski *********************************************************************/ 22086f19efc0SLukasz Majewski static int cpufreq_boost_set_sw(int state) 22096f19efc0SLukasz Majewski { 22106f19efc0SLukasz Majewski struct cpufreq_frequency_table *freq_table; 22116f19efc0SLukasz Majewski struct cpufreq_policy *policy; 22126f19efc0SLukasz Majewski int ret = -EINVAL; 22136f19efc0SLukasz Majewski 2214f963735aSViresh Kumar for_each_active_policy(policy) { 22156f19efc0SLukasz Majewski freq_table = cpufreq_frequency_get_table(policy->cpu); 22166f19efc0SLukasz Majewski if (freq_table) { 22176f19efc0SLukasz Majewski ret = cpufreq_frequency_table_cpuinfo(policy, 22186f19efc0SLukasz Majewski freq_table); 22196f19efc0SLukasz Majewski if (ret) { 22206f19efc0SLukasz Majewski pr_err("%s: Policy frequency update failed\n", 22216f19efc0SLukasz Majewski __func__); 22226f19efc0SLukasz Majewski break; 22236f19efc0SLukasz Majewski } 222449f18560SViresh Kumar 222549f18560SViresh Kumar down_write(&policy->rwsem); 22266f19efc0SLukasz Majewski policy->user_policy.max = policy->max; 2227a1317e09SViresh Kumar cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 222849f18560SViresh Kumar up_write(&policy->rwsem); 22296f19efc0SLukasz Majewski } 22306f19efc0SLukasz Majewski } 22316f19efc0SLukasz Majewski 22326f19efc0SLukasz Majewski return ret; 22336f19efc0SLukasz Majewski } 22346f19efc0SLukasz Majewski 22356f19efc0SLukasz Majewski int cpufreq_boost_trigger_state(int state) 22366f19efc0SLukasz Majewski { 22376f19efc0SLukasz Majewski unsigned long flags; 22386f19efc0SLukasz Majewski int ret = 0; 22396f19efc0SLukasz Majewski 22406f19efc0SLukasz Majewski if (cpufreq_driver->boost_enabled == state) 22416f19efc0SLukasz Majewski return 0; 22426f19efc0SLukasz Majewski 22436f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 22446f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = state; 22456f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 22466f19efc0SLukasz Majewski 22476f19efc0SLukasz Majewski ret = cpufreq_driver->set_boost(state); 22486f19efc0SLukasz Majewski if (ret) { 22496f19efc0SLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 22506f19efc0SLukasz Majewski cpufreq_driver->boost_enabled = !state; 22516f19efc0SLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 22526f19efc0SLukasz Majewski 2253e837f9b5SJoe Perches pr_err("%s: Cannot %s BOOST\n", 2254e837f9b5SJoe Perches __func__, state ? "enable" : "disable"); 22556f19efc0SLukasz Majewski } 22566f19efc0SLukasz Majewski 22576f19efc0SLukasz Majewski return ret; 22586f19efc0SLukasz Majewski } 22596f19efc0SLukasz Majewski 226041669da0SRafael J. Wysocki static bool cpufreq_boost_supported(void) 22616f19efc0SLukasz Majewski { 22627a6c79f2SRafael J. Wysocki return likely(cpufreq_driver) && cpufreq_driver->set_boost; 22636f19efc0SLukasz Majewski } 22646f19efc0SLukasz Majewski 226544139ed4SViresh Kumar static int create_boost_sysfs_file(void) 226644139ed4SViresh Kumar { 226744139ed4SViresh Kumar int ret; 226844139ed4SViresh Kumar 2269c82bd444SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, &boost.attr); 227044139ed4SViresh Kumar if (ret) 227144139ed4SViresh Kumar pr_err("%s: cannot register global BOOST sysfs file\n", 227244139ed4SViresh Kumar __func__); 227344139ed4SViresh Kumar 227444139ed4SViresh Kumar return ret; 227544139ed4SViresh Kumar } 227644139ed4SViresh Kumar 227744139ed4SViresh Kumar static void remove_boost_sysfs_file(void) 227844139ed4SViresh Kumar { 227944139ed4SViresh Kumar if (cpufreq_boost_supported()) 2280c82bd444SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, &boost.attr); 228144139ed4SViresh Kumar } 228244139ed4SViresh Kumar 228344139ed4SViresh Kumar int cpufreq_enable_boost_support(void) 228444139ed4SViresh Kumar { 228544139ed4SViresh Kumar if (!cpufreq_driver) 228644139ed4SViresh Kumar return -EINVAL; 228744139ed4SViresh Kumar 228844139ed4SViresh Kumar if (cpufreq_boost_supported()) 228944139ed4SViresh Kumar return 0; 229044139ed4SViresh Kumar 22917a6c79f2SRafael J. Wysocki cpufreq_driver->set_boost = cpufreq_boost_set_sw; 229244139ed4SViresh Kumar 229344139ed4SViresh Kumar /* This will get removed on driver unregister */ 229444139ed4SViresh Kumar return create_boost_sysfs_file(); 229544139ed4SViresh Kumar } 229644139ed4SViresh Kumar EXPORT_SYMBOL_GPL(cpufreq_enable_boost_support); 229744139ed4SViresh Kumar 22986f19efc0SLukasz Majewski int cpufreq_boost_enabled(void) 22996f19efc0SLukasz Majewski { 23006f19efc0SLukasz Majewski return cpufreq_driver->boost_enabled; 23016f19efc0SLukasz Majewski } 23026f19efc0SLukasz Majewski EXPORT_SYMBOL_GPL(cpufreq_boost_enabled); 23036f19efc0SLukasz Majewski 23046f19efc0SLukasz Majewski /********************************************************************* 23051da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 23061da177e4SLinus Torvalds *********************************************************************/ 23071da177e4SLinus Torvalds 23081da177e4SLinus Torvalds /** 23091da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 23101da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 23111da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 23121da177e4SLinus Torvalds * 23131da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 231463af4055SEric Biggers * returns zero on success, -EEXIST when another driver got here first 23151da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 23161da177e4SLinus Torvalds * 23171da177e4SLinus Torvalds */ 2318221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 23191da177e4SLinus Torvalds { 23201da177e4SLinus Torvalds unsigned long flags; 23211da177e4SLinus Torvalds int ret; 23221da177e4SLinus Torvalds 2323a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2324a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2325a7b422cdSKonrad Rzeszutek Wilk 23261da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 23279c0ebcf7SViresh Kumar !(driver_data->setpolicy || driver_data->target_index || 23289832235fSRafael J. Wysocki driver_data->target) || 23299832235fSRafael J. Wysocki (driver_data->setpolicy && (driver_data->target_index || 23301c03a2d0SViresh Kumar driver_data->target)) || 23311c03a2d0SViresh Kumar (!!driver_data->get_intermediate != !!driver_data->target_intermediate)) 23321da177e4SLinus Torvalds return -EINVAL; 23331da177e4SLinus Torvalds 23342d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 23351da177e4SLinus Torvalds 2336fdd320daSRafael J. Wysocki /* Protect against concurrent CPU online/offline. */ 2337fdd320daSRafael J. Wysocki get_online_cpus(); 2338fdd320daSRafael J. Wysocki 23390d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 23401c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 23410d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2342fdd320daSRafael J. Wysocki ret = -EEXIST; 2343fdd320daSRafael J. Wysocki goto out; 23441da177e4SLinus Torvalds } 23451c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 23460d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 23471da177e4SLinus Torvalds 2348bc68b7dfSViresh Kumar if (driver_data->setpolicy) 2349bc68b7dfSViresh Kumar driver_data->flags |= CPUFREQ_CONST_LOOPS; 2350bc68b7dfSViresh Kumar 23517a6c79f2SRafael J. Wysocki if (cpufreq_boost_supported()) { 235244139ed4SViresh Kumar ret = create_boost_sysfs_file(); 235344139ed4SViresh Kumar if (ret) 23546f19efc0SLukasz Majewski goto err_null_driver; 23557a6c79f2SRafael J. Wysocki } 23566f19efc0SLukasz Majewski 23578a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 23588f5bc2abSJiri Slaby if (ret) 23596f19efc0SLukasz Majewski goto err_boost_unreg; 23601da177e4SLinus Torvalds 2361ce1bcfe9SViresh Kumar if (!(cpufreq_driver->flags & CPUFREQ_STICKY) && 2362ce1bcfe9SViresh Kumar list_empty(&cpufreq_policy_list)) { 23631da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 2364ce1bcfe9SViresh Kumar pr_debug("%s: No CPU initialized for driver %s\n", __func__, 2365e08f5f5bSGautham R Shenoy driver_data->name); 23668a25a2fdSKay Sievers goto err_if_unreg; 23671da177e4SLinus Torvalds } 23681da177e4SLinus Torvalds 236965edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 23702d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 23711da177e4SLinus Torvalds 2372fdd320daSRafael J. Wysocki out: 2373fdd320daSRafael J. Wysocki put_online_cpus(); 2374fdd320daSRafael J. Wysocki return ret; 2375fdd320daSRafael J. Wysocki 23768a25a2fdSKay Sievers err_if_unreg: 23778a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 23786f19efc0SLukasz Majewski err_boost_unreg: 237944139ed4SViresh Kumar remove_boost_sysfs_file(); 23808f5bc2abSJiri Slaby err_null_driver: 23810d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 23821c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 23830d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2384fdd320daSRafael J. Wysocki goto out; 23851da177e4SLinus Torvalds } 23861da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 23871da177e4SLinus Torvalds 23881da177e4SLinus Torvalds /** 23891da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 23901da177e4SLinus Torvalds * 23911da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 23921da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 23931da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 23941da177e4SLinus Torvalds * currently not initialised. 23951da177e4SLinus Torvalds */ 2396221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 23971da177e4SLinus Torvalds { 23981da177e4SLinus Torvalds unsigned long flags; 23991da177e4SLinus Torvalds 24001c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 24011da177e4SLinus Torvalds return -EINVAL; 24021da177e4SLinus Torvalds 24032d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 24041da177e4SLinus Torvalds 2405454d3a25SSebastian Andrzej Siewior /* Protect against concurrent cpu hotplug */ 2406454d3a25SSebastian Andrzej Siewior get_online_cpus(); 24078a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 240844139ed4SViresh Kumar remove_boost_sysfs_file(); 240965edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 24101da177e4SLinus Torvalds 24110d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 24126eed9404SViresh Kumar 24131c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 24146eed9404SViresh Kumar 24150d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2416454d3a25SSebastian Andrzej Siewior put_online_cpus(); 24171da177e4SLinus Torvalds 24181da177e4SLinus Torvalds return 0; 24191da177e4SLinus Torvalds } 24201da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 24215a01f2e8SVenkatesh Pallipadi 242290de2a4aSDoug Anderson /* 242390de2a4aSDoug Anderson * Stop cpufreq at shutdown to make sure it isn't holding any locks 242490de2a4aSDoug Anderson * or mutexes when secondary CPUs are halted. 242590de2a4aSDoug Anderson */ 242690de2a4aSDoug Anderson static struct syscore_ops cpufreq_syscore_ops = { 242790de2a4aSDoug Anderson .shutdown = cpufreq_suspend, 242890de2a4aSDoug Anderson }; 242990de2a4aSDoug Anderson 2430c82bd444SViresh Kumar struct kobject *cpufreq_global_kobject; 2431c82bd444SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 2432c82bd444SViresh Kumar 24335a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 24345a01f2e8SVenkatesh Pallipadi { 2435a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2436a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2437a7b422cdSKonrad Rzeszutek Wilk 24388eec1020SViresh Kumar cpufreq_global_kobject = kobject_create_and_add("cpufreq", &cpu_subsys.dev_root->kobj); 24398aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 24408aa84ad8SThomas Renninger 244190de2a4aSDoug Anderson register_syscore_ops(&cpufreq_syscore_ops); 244290de2a4aSDoug Anderson 24435a01f2e8SVenkatesh Pallipadi return 0; 24445a01f2e8SVenkatesh Pallipadi } 24455a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2446