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> 29e00e56dfSRafael J. Wysocki #include <linux/syscore_ops.h> 305ff0a268SViresh Kumar #include <linux/tick.h> 316f4f2723SThomas Renninger #include <trace/events/power.h> 326f4f2723SThomas Renninger 331da177e4SLinus Torvalds /** 34cd878479SDave Jones * The "cpufreq driver" - the arch- or hardware-dependent low 351da177e4SLinus Torvalds * level driver of CPUFreq support, and its spinlock. This lock 361da177e4SLinus Torvalds * also protects the cpufreq_cpu_data array. 371da177e4SLinus Torvalds */ 381c3d85ddSRafael J. Wysocki static struct cpufreq_driver *cpufreq_driver; 397a6aedfaSMike Travis static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 408414809cSSrivatsa S. Bhat static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback); 41bb176f7dSViresh Kumar static DEFINE_RWLOCK(cpufreq_driver_lock); 42bb176f7dSViresh Kumar static DEFINE_MUTEX(cpufreq_governor_lock); 43c88a1f8bSLukasz Majewski static LIST_HEAD(cpufreq_policy_list); 44bb176f7dSViresh Kumar 45084f3493SThomas Renninger #ifdef CONFIG_HOTPLUG_CPU 46084f3493SThomas Renninger /* This one keeps track of the previously set governor of a removed CPU */ 47e77b89f1SDmitry Monakhov static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor); 48084f3493SThomas Renninger #endif 491da177e4SLinus Torvalds 505a01f2e8SVenkatesh Pallipadi /* 515a01f2e8SVenkatesh Pallipadi * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure 525a01f2e8SVenkatesh Pallipadi * all cpufreq/hotplug/workqueue/etc related lock issues. 535a01f2e8SVenkatesh Pallipadi * 545a01f2e8SVenkatesh Pallipadi * The rules for this semaphore: 555a01f2e8SVenkatesh Pallipadi * - Any routine that wants to read from the policy structure will 565a01f2e8SVenkatesh Pallipadi * do a down_read on this semaphore. 575a01f2e8SVenkatesh Pallipadi * - Any routine that will write to the policy structure and/or may take away 585a01f2e8SVenkatesh Pallipadi * the policy altogether (eg. CPU hotplug), will hold this lock in write 595a01f2e8SVenkatesh Pallipadi * mode before doing so. 605a01f2e8SVenkatesh Pallipadi * 615a01f2e8SVenkatesh Pallipadi * Additional rules: 625a01f2e8SVenkatesh Pallipadi * - Governor routines that can be called in cpufreq hotplug path should not 635a01f2e8SVenkatesh Pallipadi * take this sem as top level hotplug notifier handler takes this. 64395913d0SMathieu Desnoyers * - Lock should not be held across 65395913d0SMathieu Desnoyers * __cpufreq_governor(data, CPUFREQ_GOV_STOP); 665a01f2e8SVenkatesh Pallipadi */ 675a01f2e8SVenkatesh Pallipadi static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem); 685a01f2e8SVenkatesh Pallipadi 695a01f2e8SVenkatesh Pallipadi #define lock_policy_rwsem(mode, cpu) \ 70fa1d8af4SViresh Kumar static int lock_policy_rwsem_##mode(int cpu) \ 715a01f2e8SVenkatesh Pallipadi { \ 72*474deff7SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \ 73*474deff7SViresh Kumar BUG_ON(!policy); \ 74*474deff7SViresh Kumar down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \ 755a01f2e8SVenkatesh Pallipadi \ 765a01f2e8SVenkatesh Pallipadi return 0; \ 775a01f2e8SVenkatesh Pallipadi } 785a01f2e8SVenkatesh Pallipadi 795a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(read, cpu); 805a01f2e8SVenkatesh Pallipadi lock_policy_rwsem(write, cpu); 815a01f2e8SVenkatesh Pallipadi 82fa1d8af4SViresh Kumar #define unlock_policy_rwsem(mode, cpu) \ 83fa1d8af4SViresh Kumar static void unlock_policy_rwsem_##mode(int cpu) \ 84fa1d8af4SViresh Kumar { \ 85*474deff7SViresh Kumar struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \ 86*474deff7SViresh Kumar BUG_ON(!policy); \ 87*474deff7SViresh Kumar up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \ 885a01f2e8SVenkatesh Pallipadi } 895a01f2e8SVenkatesh Pallipadi 90fa1d8af4SViresh Kumar unlock_policy_rwsem(read, cpu); 91fa1d8af4SViresh Kumar unlock_policy_rwsem(write, cpu); 925a01f2e8SVenkatesh Pallipadi 936eed9404SViresh Kumar /* 946eed9404SViresh Kumar * rwsem to guarantee that cpufreq driver module doesn't unload during critical 956eed9404SViresh Kumar * sections 966eed9404SViresh Kumar */ 976eed9404SViresh Kumar static DECLARE_RWSEM(cpufreq_rwsem); 986eed9404SViresh Kumar 991da177e4SLinus Torvalds /* internal prototypes */ 10029464f28SDave Jones static int __cpufreq_governor(struct cpufreq_policy *policy, 10129464f28SDave Jones unsigned int event); 1025a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu); 10365f27f38SDavid Howells static void handle_update(struct work_struct *work); 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /** 1061da177e4SLinus Torvalds * Two notifier lists: the "policy" list is involved in the 1071da177e4SLinus Torvalds * validation process for a new CPU frequency policy; the 1081da177e4SLinus Torvalds * "transition" list for kernel code that needs to handle 1091da177e4SLinus Torvalds * changes to devices when the CPU clock speed changes. 1101da177e4SLinus Torvalds * The mutex locks both lists. 1111da177e4SLinus Torvalds */ 112e041c683SAlan Stern static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 113b4dfdbb3SAlan Stern static struct srcu_notifier_head cpufreq_transition_notifier_list; 1141da177e4SLinus Torvalds 11574212ca4SCesar Eduardo Barros static bool init_cpufreq_transition_notifier_list_called; 116b4dfdbb3SAlan Stern static int __init init_cpufreq_transition_notifier_list(void) 117b4dfdbb3SAlan Stern { 118b4dfdbb3SAlan Stern srcu_init_notifier_head(&cpufreq_transition_notifier_list); 11974212ca4SCesar Eduardo Barros init_cpufreq_transition_notifier_list_called = true; 120b4dfdbb3SAlan Stern return 0; 121b4dfdbb3SAlan Stern } 122b3438f82SLinus Torvalds pure_initcall(init_cpufreq_transition_notifier_list); 1231da177e4SLinus Torvalds 124a7b422cdSKonrad Rzeszutek Wilk static int off __read_mostly; 125da584455SViresh Kumar static int cpufreq_disabled(void) 126a7b422cdSKonrad Rzeszutek Wilk { 127a7b422cdSKonrad Rzeszutek Wilk return off; 128a7b422cdSKonrad Rzeszutek Wilk } 129a7b422cdSKonrad Rzeszutek Wilk void disable_cpufreq(void) 130a7b422cdSKonrad Rzeszutek Wilk { 131a7b422cdSKonrad Rzeszutek Wilk off = 1; 132a7b422cdSKonrad Rzeszutek Wilk } 1331da177e4SLinus Torvalds static LIST_HEAD(cpufreq_governor_list); 1343fc54d37Sakpm@osdl.org static DEFINE_MUTEX(cpufreq_governor_mutex); 1351da177e4SLinus Torvalds 1364d5dcc42SViresh Kumar bool have_governor_per_policy(void) 1374d5dcc42SViresh Kumar { 1381c3d85ddSRafael J. Wysocki return cpufreq_driver->have_governor_per_policy; 1394d5dcc42SViresh Kumar } 1403f869d6dSViresh Kumar EXPORT_SYMBOL_GPL(have_governor_per_policy); 1414d5dcc42SViresh Kumar 142944e9a03SViresh Kumar struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy) 143944e9a03SViresh Kumar { 144944e9a03SViresh Kumar if (have_governor_per_policy()) 145944e9a03SViresh Kumar return &policy->kobj; 146944e9a03SViresh Kumar else 147944e9a03SViresh Kumar return cpufreq_global_kobject; 148944e9a03SViresh Kumar } 149944e9a03SViresh Kumar EXPORT_SYMBOL_GPL(get_governor_parent_kobj); 150944e9a03SViresh Kumar 15172a4ce34SViresh Kumar static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall) 15272a4ce34SViresh Kumar { 15372a4ce34SViresh Kumar u64 idle_time; 15472a4ce34SViresh Kumar u64 cur_wall_time; 15572a4ce34SViresh Kumar u64 busy_time; 15672a4ce34SViresh Kumar 15772a4ce34SViresh Kumar cur_wall_time = jiffies64_to_cputime64(get_jiffies_64()); 15872a4ce34SViresh Kumar 15972a4ce34SViresh Kumar busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER]; 16072a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM]; 16172a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ]; 16272a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ]; 16372a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL]; 16472a4ce34SViresh Kumar busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE]; 16572a4ce34SViresh Kumar 16672a4ce34SViresh Kumar idle_time = cur_wall_time - busy_time; 16772a4ce34SViresh Kumar if (wall) 16872a4ce34SViresh Kumar *wall = cputime_to_usecs(cur_wall_time); 16972a4ce34SViresh Kumar 17072a4ce34SViresh Kumar return cputime_to_usecs(idle_time); 17172a4ce34SViresh Kumar } 17272a4ce34SViresh Kumar 17372a4ce34SViresh Kumar u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy) 17472a4ce34SViresh Kumar { 17572a4ce34SViresh Kumar u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL); 17672a4ce34SViresh Kumar 17772a4ce34SViresh Kumar if (idle_time == -1ULL) 17872a4ce34SViresh Kumar return get_cpu_idle_time_jiffy(cpu, wall); 17972a4ce34SViresh Kumar else if (!io_busy) 18072a4ce34SViresh Kumar idle_time += get_cpu_iowait_time_us(cpu, wall); 18172a4ce34SViresh Kumar 18272a4ce34SViresh Kumar return idle_time; 18372a4ce34SViresh Kumar } 18472a4ce34SViresh Kumar EXPORT_SYMBOL_GPL(get_cpu_idle_time); 18572a4ce34SViresh Kumar 1866eed9404SViresh Kumar struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 1871da177e4SLinus Torvalds { 1886eed9404SViresh Kumar struct cpufreq_policy *policy = NULL; 1891da177e4SLinus Torvalds unsigned long flags; 1901da177e4SLinus Torvalds 1916eed9404SViresh Kumar if (cpufreq_disabled() || (cpu >= nr_cpu_ids)) 1926eed9404SViresh Kumar return NULL; 1936eed9404SViresh Kumar 1946eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 1956eed9404SViresh Kumar return NULL; 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* get the cpufreq driver */ 1980d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 1991da177e4SLinus Torvalds 2006eed9404SViresh Kumar if (cpufreq_driver) { 2011da177e4SLinus Torvalds /* get the CPU */ 2023a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 2036eed9404SViresh Kumar if (policy) 2046eed9404SViresh Kumar kobject_get(&policy->kobj); 2056eed9404SViresh Kumar } 2066eed9404SViresh Kumar 2076eed9404SViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 2081da177e4SLinus Torvalds 2093a3e9e06SViresh Kumar if (!policy) 2106eed9404SViresh Kumar up_read(&cpufreq_rwsem); 2111da177e4SLinus Torvalds 2123a3e9e06SViresh Kumar return policy; 213a9144436SStephen Boyd } 2141da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 2151da177e4SLinus Torvalds 2163a3e9e06SViresh Kumar void cpufreq_cpu_put(struct cpufreq_policy *policy) 217a9144436SStephen Boyd { 218d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 219d5aaffa9SDirk Brandewie return; 220d5aaffa9SDirk Brandewie 2216eed9404SViresh Kumar kobject_put(&policy->kobj); 2226eed9404SViresh Kumar up_read(&cpufreq_rwsem); 223a9144436SStephen Boyd } 2241da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 2251da177e4SLinus Torvalds 2261da177e4SLinus Torvalds /********************************************************************* 2271da177e4SLinus Torvalds * EXTERNALLY AFFECTING FREQUENCY CHANGES * 2281da177e4SLinus Torvalds *********************************************************************/ 2291da177e4SLinus Torvalds 2301da177e4SLinus Torvalds /** 2311da177e4SLinus Torvalds * adjust_jiffies - adjust the system "loops_per_jiffy" 2321da177e4SLinus Torvalds * 2331da177e4SLinus Torvalds * This function alters the system "loops_per_jiffy" for the clock 2341da177e4SLinus Torvalds * speed change. Note that loops_per_jiffy cannot be updated on SMP 2351da177e4SLinus Torvalds * systems as each CPU might be scaled differently. So, use the arch 2361da177e4SLinus Torvalds * per-CPU loops_per_jiffy value wherever possible. 2371da177e4SLinus Torvalds */ 2381da177e4SLinus Torvalds #ifndef CONFIG_SMP 2391da177e4SLinus Torvalds static unsigned long l_p_j_ref; 2401da177e4SLinus Torvalds static unsigned int l_p_j_ref_freq; 2411da177e4SLinus Torvalds 242858119e1SArjan van de Ven static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 2431da177e4SLinus Torvalds { 2441da177e4SLinus Torvalds if (ci->flags & CPUFREQ_CONST_LOOPS) 2451da177e4SLinus Torvalds return; 2461da177e4SLinus Torvalds 2471da177e4SLinus Torvalds if (!l_p_j_ref_freq) { 2481da177e4SLinus Torvalds l_p_j_ref = loops_per_jiffy; 2491da177e4SLinus Torvalds l_p_j_ref_freq = ci->old; 2502d06d8c4SDominik Brodowski pr_debug("saving %lu as reference value for loops_per_jiffy; " 251e08f5f5bSGautham R Shenoy "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 2521da177e4SLinus Torvalds } 253d08de0c1SAfzal Mohammed if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) || 25442d4dc3fSBenjamin Herrenschmidt (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 255e08f5f5bSGautham R Shenoy loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 256e08f5f5bSGautham R Shenoy ci->new); 2572d06d8c4SDominik Brodowski pr_debug("scaling loops_per_jiffy to %lu " 258e08f5f5bSGautham R Shenoy "for frequency %u kHz\n", loops_per_jiffy, ci->new); 2591da177e4SLinus Torvalds } 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds #else 262e08f5f5bSGautham R Shenoy static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 263e08f5f5bSGautham R Shenoy { 264e08f5f5bSGautham R Shenoy return; 265e08f5f5bSGautham R Shenoy } 2661da177e4SLinus Torvalds #endif 2671da177e4SLinus Torvalds 2680956df9cSViresh Kumar static void __cpufreq_notify_transition(struct cpufreq_policy *policy, 269b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 2701da177e4SLinus Torvalds { 2711da177e4SLinus Torvalds BUG_ON(irqs_disabled()); 2721da177e4SLinus Torvalds 273d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 274d5aaffa9SDirk Brandewie return; 275d5aaffa9SDirk Brandewie 2761c3d85ddSRafael J. Wysocki freqs->flags = cpufreq_driver->flags; 2772d06d8c4SDominik Brodowski pr_debug("notification %u of frequency transition to %u kHz\n", 278e4472cb3SDave Jones state, freqs->new); 2791da177e4SLinus Torvalds 2801da177e4SLinus Torvalds switch (state) { 281e4472cb3SDave Jones 2821da177e4SLinus Torvalds case CPUFREQ_PRECHANGE: 283266c13d7SViresh Kumar if (WARN(policy->transition_ongoing == 284266c13d7SViresh Kumar cpumask_weight(policy->cpus), 2857c30ed53SViresh Kumar "In middle of another frequency transition\n")) 2867c30ed53SViresh Kumar return; 2877c30ed53SViresh Kumar 288266c13d7SViresh Kumar policy->transition_ongoing++; 2897c30ed53SViresh Kumar 290e4472cb3SDave Jones /* detect if the driver reported a value as "old frequency" 291e4472cb3SDave Jones * which is not equal to what the cpufreq core thinks is 292e4472cb3SDave Jones * "old frequency". 2931da177e4SLinus Torvalds */ 2941c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 295e4472cb3SDave Jones if ((policy) && (policy->cpu == freqs->cpu) && 296e4472cb3SDave Jones (policy->cur) && (policy->cur != freqs->old)) { 2972d06d8c4SDominik Brodowski pr_debug("Warning: CPU frequency is" 298e4472cb3SDave Jones " %u, cpufreq assumed %u kHz.\n", 299e4472cb3SDave Jones freqs->old, policy->cur); 300e4472cb3SDave Jones freqs->old = policy->cur; 3011da177e4SLinus Torvalds } 3021da177e4SLinus Torvalds } 303b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 304e4472cb3SDave Jones CPUFREQ_PRECHANGE, freqs); 3051da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 3061da177e4SLinus Torvalds break; 307e4472cb3SDave Jones 3081da177e4SLinus Torvalds case CPUFREQ_POSTCHANGE: 3097c30ed53SViresh Kumar if (WARN(!policy->transition_ongoing, 3107c30ed53SViresh Kumar "No frequency transition in progress\n")) 3117c30ed53SViresh Kumar return; 3127c30ed53SViresh Kumar 313266c13d7SViresh Kumar policy->transition_ongoing--; 3147c30ed53SViresh Kumar 3151da177e4SLinus Torvalds adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 3162d06d8c4SDominik Brodowski pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new, 3176f4f2723SThomas Renninger (unsigned long)freqs->cpu); 31825e41933SThomas Renninger trace_cpu_frequency(freqs->new, freqs->cpu); 319b4dfdbb3SAlan Stern srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 320e4472cb3SDave Jones CPUFREQ_POSTCHANGE, freqs); 321e4472cb3SDave Jones if (likely(policy) && likely(policy->cpu == freqs->cpu)) 322e4472cb3SDave Jones policy->cur = freqs->new; 3231da177e4SLinus Torvalds break; 3241da177e4SLinus Torvalds } 3251da177e4SLinus Torvalds } 326bb176f7dSViresh Kumar 327b43a7ffbSViresh Kumar /** 328b43a7ffbSViresh Kumar * cpufreq_notify_transition - call notifier chain and adjust_jiffies 329b43a7ffbSViresh Kumar * on frequency transition. 330b43a7ffbSViresh Kumar * 331b43a7ffbSViresh Kumar * This function calls the transition notifiers and the "adjust_jiffies" 332b43a7ffbSViresh Kumar * function. It is called twice on all CPU frequency changes that have 333b43a7ffbSViresh Kumar * external effects. 334b43a7ffbSViresh Kumar */ 335b43a7ffbSViresh Kumar void cpufreq_notify_transition(struct cpufreq_policy *policy, 336b43a7ffbSViresh Kumar struct cpufreq_freqs *freqs, unsigned int state) 337b43a7ffbSViresh Kumar { 338b43a7ffbSViresh Kumar for_each_cpu(freqs->cpu, policy->cpus) 339b43a7ffbSViresh Kumar __cpufreq_notify_transition(policy, freqs, state); 340b43a7ffbSViresh Kumar } 3411da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds 3441da177e4SLinus Torvalds /********************************************************************* 3451da177e4SLinus Torvalds * SYSFS INTERFACE * 3461da177e4SLinus Torvalds *********************************************************************/ 3471da177e4SLinus Torvalds 3483bcb09a3SJeremy Fitzhardinge static struct cpufreq_governor *__find_governor(const char *str_governor) 3493bcb09a3SJeremy Fitzhardinge { 3503bcb09a3SJeremy Fitzhardinge struct cpufreq_governor *t; 3513bcb09a3SJeremy Fitzhardinge 3523bcb09a3SJeremy Fitzhardinge list_for_each_entry(t, &cpufreq_governor_list, governor_list) 3533bcb09a3SJeremy Fitzhardinge if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 3543bcb09a3SJeremy Fitzhardinge return t; 3553bcb09a3SJeremy Fitzhardinge 3563bcb09a3SJeremy Fitzhardinge return NULL; 3573bcb09a3SJeremy Fitzhardinge } 3583bcb09a3SJeremy Fitzhardinge 3591da177e4SLinus Torvalds /** 3601da177e4SLinus Torvalds * cpufreq_parse_governor - parse a governor string 3611da177e4SLinus Torvalds */ 3621da177e4SLinus Torvalds static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 3631da177e4SLinus Torvalds struct cpufreq_governor **governor) 3641da177e4SLinus Torvalds { 3653bcb09a3SJeremy Fitzhardinge int err = -EINVAL; 3663bcb09a3SJeremy Fitzhardinge 3671c3d85ddSRafael J. Wysocki if (!cpufreq_driver) 3683bcb09a3SJeremy Fitzhardinge goto out; 3693bcb09a3SJeremy Fitzhardinge 3701c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 3711da177e4SLinus Torvalds if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 3721da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_PERFORMANCE; 3733bcb09a3SJeremy Fitzhardinge err = 0; 374e08f5f5bSGautham R Shenoy } else if (!strnicmp(str_governor, "powersave", 375e08f5f5bSGautham R Shenoy CPUFREQ_NAME_LEN)) { 3761da177e4SLinus Torvalds *policy = CPUFREQ_POLICY_POWERSAVE; 3773bcb09a3SJeremy Fitzhardinge err = 0; 3781da177e4SLinus Torvalds } 3791c3d85ddSRafael J. Wysocki } else if (cpufreq_driver->target) { 3801da177e4SLinus Torvalds struct cpufreq_governor *t; 3813bcb09a3SJeremy Fitzhardinge 3823fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 3833bcb09a3SJeremy Fitzhardinge 3843bcb09a3SJeremy Fitzhardinge t = __find_governor(str_governor); 3853bcb09a3SJeremy Fitzhardinge 386ea714970SJeremy Fitzhardinge if (t == NULL) { 387ea714970SJeremy Fitzhardinge int ret; 388ea714970SJeremy Fitzhardinge 389ea714970SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 3901a8e1463SKees Cook ret = request_module("cpufreq_%s", str_governor); 391ea714970SJeremy Fitzhardinge mutex_lock(&cpufreq_governor_mutex); 392ea714970SJeremy Fitzhardinge 393ea714970SJeremy Fitzhardinge if (ret == 0) 394ea714970SJeremy Fitzhardinge t = __find_governor(str_governor); 395ea714970SJeremy Fitzhardinge } 396ea714970SJeremy Fitzhardinge 3973bcb09a3SJeremy Fitzhardinge if (t != NULL) { 3981da177e4SLinus Torvalds *governor = t; 3993bcb09a3SJeremy Fitzhardinge err = 0; 4001da177e4SLinus Torvalds } 4013bcb09a3SJeremy Fitzhardinge 4023bcb09a3SJeremy Fitzhardinge mutex_unlock(&cpufreq_governor_mutex); 4031da177e4SLinus Torvalds } 4041da177e4SLinus Torvalds out: 4053bcb09a3SJeremy Fitzhardinge return err; 4061da177e4SLinus Torvalds } 4071da177e4SLinus Torvalds 4081da177e4SLinus Torvalds /** 409e08f5f5bSGautham R Shenoy * cpufreq_per_cpu_attr_read() / show_##file_name() - 410e08f5f5bSGautham R Shenoy * print out cpufreq information 4111da177e4SLinus Torvalds * 4121da177e4SLinus Torvalds * Write out information from cpufreq_driver->policy[cpu]; object must be 4131da177e4SLinus Torvalds * "unsigned int". 4141da177e4SLinus Torvalds */ 4151da177e4SLinus Torvalds 4161da177e4SLinus Torvalds #define show_one(file_name, object) \ 4171da177e4SLinus Torvalds static ssize_t show_##file_name \ 4181da177e4SLinus Torvalds (struct cpufreq_policy *policy, char *buf) \ 4191da177e4SLinus Torvalds { \ 4201da177e4SLinus Torvalds return sprintf(buf, "%u\n", policy->object); \ 4211da177e4SLinus Torvalds } 4221da177e4SLinus Torvalds 4231da177e4SLinus Torvalds show_one(cpuinfo_min_freq, cpuinfo.min_freq); 4241da177e4SLinus Torvalds show_one(cpuinfo_max_freq, cpuinfo.max_freq); 425ed129784SThomas Renninger show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 4261da177e4SLinus Torvalds show_one(scaling_min_freq, min); 4271da177e4SLinus Torvalds show_one(scaling_max_freq, max); 4281da177e4SLinus Torvalds show_one(scaling_cur_freq, cur); 4291da177e4SLinus Torvalds 4303a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy, 4313a3e9e06SViresh Kumar struct cpufreq_policy *new_policy); 4327970e08bSThomas Renninger 4331da177e4SLinus Torvalds /** 4341da177e4SLinus Torvalds * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 4351da177e4SLinus Torvalds */ 4361da177e4SLinus Torvalds #define store_one(file_name, object) \ 4371da177e4SLinus Torvalds static ssize_t store_##file_name \ 4381da177e4SLinus Torvalds (struct cpufreq_policy *policy, const char *buf, size_t count) \ 4391da177e4SLinus Torvalds { \ 440f55c9c26SJingoo Han unsigned int ret; \ 4411da177e4SLinus Torvalds struct cpufreq_policy new_policy; \ 4421da177e4SLinus Torvalds \ 4431da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 4441da177e4SLinus Torvalds if (ret) \ 4451da177e4SLinus Torvalds return -EINVAL; \ 4461da177e4SLinus Torvalds \ 4471da177e4SLinus Torvalds ret = sscanf(buf, "%u", &new_policy.object); \ 4481da177e4SLinus Torvalds if (ret != 1) \ 4491da177e4SLinus Torvalds return -EINVAL; \ 4501da177e4SLinus Torvalds \ 4517970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); \ 4527970e08bSThomas Renninger policy->user_policy.object = policy->object; \ 4531da177e4SLinus Torvalds \ 4541da177e4SLinus Torvalds return ret ? ret : count; \ 4551da177e4SLinus Torvalds } 4561da177e4SLinus Torvalds 4571da177e4SLinus Torvalds store_one(scaling_min_freq, min); 4581da177e4SLinus Torvalds store_one(scaling_max_freq, max); 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /** 4611da177e4SLinus Torvalds * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 4621da177e4SLinus Torvalds */ 463e08f5f5bSGautham R Shenoy static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 464e08f5f5bSGautham R Shenoy char *buf) 4651da177e4SLinus Torvalds { 4665a01f2e8SVenkatesh Pallipadi unsigned int cur_freq = __cpufreq_get(policy->cpu); 4671da177e4SLinus Torvalds if (!cur_freq) 4681da177e4SLinus Torvalds return sprintf(buf, "<unknown>"); 4691da177e4SLinus Torvalds return sprintf(buf, "%u\n", cur_freq); 4701da177e4SLinus Torvalds } 4711da177e4SLinus Torvalds 4721da177e4SLinus Torvalds /** 4731da177e4SLinus Torvalds * show_scaling_governor - show the current policy for the specified CPU 4741da177e4SLinus Torvalds */ 475905d77cdSDave Jones static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 4761da177e4SLinus Torvalds { 4771da177e4SLinus Torvalds if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 4781da177e4SLinus Torvalds return sprintf(buf, "powersave\n"); 4791da177e4SLinus Torvalds else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 4801da177e4SLinus Torvalds return sprintf(buf, "performance\n"); 4811da177e4SLinus Torvalds else if (policy->governor) 4824b972f0bSviresh kumar return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", 48329464f28SDave Jones policy->governor->name); 4841da177e4SLinus Torvalds return -EINVAL; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /** 4881da177e4SLinus Torvalds * store_scaling_governor - store policy for the specified CPU 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 4911da177e4SLinus Torvalds const char *buf, size_t count) 4921da177e4SLinus Torvalds { 493f55c9c26SJingoo Han unsigned int ret; 4941da177e4SLinus Torvalds char str_governor[16]; 4951da177e4SLinus Torvalds struct cpufreq_policy new_policy; 4961da177e4SLinus Torvalds 4971da177e4SLinus Torvalds ret = cpufreq_get_policy(&new_policy, policy->cpu); 4981da177e4SLinus Torvalds if (ret) 4991da177e4SLinus Torvalds return ret; 5001da177e4SLinus Torvalds 5011da177e4SLinus Torvalds ret = sscanf(buf, "%15s", str_governor); 5021da177e4SLinus Torvalds if (ret != 1) 5031da177e4SLinus Torvalds return -EINVAL; 5041da177e4SLinus Torvalds 505e08f5f5bSGautham R Shenoy if (cpufreq_parse_governor(str_governor, &new_policy.policy, 506e08f5f5bSGautham R Shenoy &new_policy.governor)) 5071da177e4SLinus Torvalds return -EINVAL; 5081da177e4SLinus Torvalds 509bb176f7dSViresh Kumar /* 510bb176f7dSViresh Kumar * Do not use cpufreq_set_policy here or the user_policy.max 511bb176f7dSViresh Kumar * will be wrongly overridden 512bb176f7dSViresh Kumar */ 5137970e08bSThomas Renninger ret = __cpufreq_set_policy(policy, &new_policy); 5147970e08bSThomas Renninger 5157970e08bSThomas Renninger policy->user_policy.policy = policy->policy; 5167970e08bSThomas Renninger policy->user_policy.governor = policy->governor; 5177970e08bSThomas Renninger 518e08f5f5bSGautham R Shenoy if (ret) 519e08f5f5bSGautham R Shenoy return ret; 520e08f5f5bSGautham R Shenoy else 521e08f5f5bSGautham R Shenoy return count; 5221da177e4SLinus Torvalds } 5231da177e4SLinus Torvalds 5241da177e4SLinus Torvalds /** 5251da177e4SLinus Torvalds * show_scaling_driver - show the cpufreq driver currently loaded 5261da177e4SLinus Torvalds */ 5271da177e4SLinus Torvalds static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 5281da177e4SLinus Torvalds { 5291c3d85ddSRafael J. Wysocki return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name); 5301da177e4SLinus Torvalds } 5311da177e4SLinus Torvalds 5321da177e4SLinus Torvalds /** 5331da177e4SLinus Torvalds * show_scaling_available_governors - show the available CPUfreq governors 5341da177e4SLinus Torvalds */ 5351da177e4SLinus Torvalds static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 5361da177e4SLinus Torvalds char *buf) 5371da177e4SLinus Torvalds { 5381da177e4SLinus Torvalds ssize_t i = 0; 5391da177e4SLinus Torvalds struct cpufreq_governor *t; 5401da177e4SLinus Torvalds 5411c3d85ddSRafael J. Wysocki if (!cpufreq_driver->target) { 5421da177e4SLinus Torvalds i += sprintf(buf, "performance powersave"); 5431da177e4SLinus Torvalds goto out; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 54729464f28SDave Jones if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 54829464f28SDave Jones - (CPUFREQ_NAME_LEN + 2))) 5491da177e4SLinus Torvalds goto out; 5504b972f0bSviresh kumar i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name); 5511da177e4SLinus Torvalds } 5521da177e4SLinus Torvalds out: 5531da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5541da177e4SLinus Torvalds return i; 5551da177e4SLinus Torvalds } 556e8628dd0SDarrick J. Wong 557f4fd3797SLan Tianyu ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf) 5581da177e4SLinus Torvalds { 5591da177e4SLinus Torvalds ssize_t i = 0; 5601da177e4SLinus Torvalds unsigned int cpu; 5611da177e4SLinus Torvalds 562835481d9SRusty Russell for_each_cpu(cpu, mask) { 5631da177e4SLinus Torvalds if (i) 5641da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 5651da177e4SLinus Torvalds i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 5661da177e4SLinus Torvalds if (i >= (PAGE_SIZE - 5)) 5671da177e4SLinus Torvalds break; 5681da177e4SLinus Torvalds } 5691da177e4SLinus Torvalds i += sprintf(&buf[i], "\n"); 5701da177e4SLinus Torvalds return i; 5711da177e4SLinus Torvalds } 572f4fd3797SLan Tianyu EXPORT_SYMBOL_GPL(cpufreq_show_cpus); 5731da177e4SLinus Torvalds 574e8628dd0SDarrick J. Wong /** 575e8628dd0SDarrick J. Wong * show_related_cpus - show the CPUs affected by each transition even if 576e8628dd0SDarrick J. Wong * hw coordination is in use 577e8628dd0SDarrick J. Wong */ 578e8628dd0SDarrick J. Wong static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 579e8628dd0SDarrick J. Wong { 580f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->related_cpus, buf); 581e8628dd0SDarrick J. Wong } 582e8628dd0SDarrick J. Wong 583e8628dd0SDarrick J. Wong /** 584e8628dd0SDarrick J. Wong * show_affected_cpus - show the CPUs affected by each transition 585e8628dd0SDarrick J. Wong */ 586e8628dd0SDarrick J. Wong static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 587e8628dd0SDarrick J. Wong { 588f4fd3797SLan Tianyu return cpufreq_show_cpus(policy->cpus, buf); 589e8628dd0SDarrick J. Wong } 590e8628dd0SDarrick J. Wong 5919e76988eSVenki Pallipadi static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 5929e76988eSVenki Pallipadi const char *buf, size_t count) 5939e76988eSVenki Pallipadi { 5949e76988eSVenki Pallipadi unsigned int freq = 0; 5959e76988eSVenki Pallipadi unsigned int ret; 5969e76988eSVenki Pallipadi 597879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->store_setspeed) 5989e76988eSVenki Pallipadi return -EINVAL; 5999e76988eSVenki Pallipadi 6009e76988eSVenki Pallipadi ret = sscanf(buf, "%u", &freq); 6019e76988eSVenki Pallipadi if (ret != 1) 6029e76988eSVenki Pallipadi return -EINVAL; 6039e76988eSVenki Pallipadi 6049e76988eSVenki Pallipadi policy->governor->store_setspeed(policy, freq); 6059e76988eSVenki Pallipadi 6069e76988eSVenki Pallipadi return count; 6079e76988eSVenki Pallipadi } 6089e76988eSVenki Pallipadi 6099e76988eSVenki Pallipadi static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 6109e76988eSVenki Pallipadi { 611879000f9SCHIKAMA masaki if (!policy->governor || !policy->governor->show_setspeed) 6129e76988eSVenki Pallipadi return sprintf(buf, "<unsupported>\n"); 6139e76988eSVenki Pallipadi 6149e76988eSVenki Pallipadi return policy->governor->show_setspeed(policy, buf); 6159e76988eSVenki Pallipadi } 6161da177e4SLinus Torvalds 617e2f74f35SThomas Renninger /** 6188bf1ac72Sviresh kumar * show_bios_limit - show the current cpufreq HW/BIOS limitation 619e2f74f35SThomas Renninger */ 620e2f74f35SThomas Renninger static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 621e2f74f35SThomas Renninger { 622e2f74f35SThomas Renninger unsigned int limit; 623e2f74f35SThomas Renninger int ret; 6241c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 6251c3d85ddSRafael J. Wysocki ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 626e2f74f35SThomas Renninger if (!ret) 627e2f74f35SThomas Renninger return sprintf(buf, "%u\n", limit); 628e2f74f35SThomas Renninger } 629e2f74f35SThomas Renninger return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 630e2f74f35SThomas Renninger } 631e2f74f35SThomas Renninger 6326dad2a29SBorislav Petkov cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 6336dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_min_freq); 6346dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_max_freq); 6356dad2a29SBorislav Petkov cpufreq_freq_attr_ro(cpuinfo_transition_latency); 6366dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_available_governors); 6376dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_driver); 6386dad2a29SBorislav Petkov cpufreq_freq_attr_ro(scaling_cur_freq); 6396dad2a29SBorislav Petkov cpufreq_freq_attr_ro(bios_limit); 6406dad2a29SBorislav Petkov cpufreq_freq_attr_ro(related_cpus); 6416dad2a29SBorislav Petkov cpufreq_freq_attr_ro(affected_cpus); 6426dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_min_freq); 6436dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_max_freq); 6446dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_governor); 6456dad2a29SBorislav Petkov cpufreq_freq_attr_rw(scaling_setspeed); 6461da177e4SLinus Torvalds 6471da177e4SLinus Torvalds static struct attribute *default_attrs[] = { 6481da177e4SLinus Torvalds &cpuinfo_min_freq.attr, 6491da177e4SLinus Torvalds &cpuinfo_max_freq.attr, 650ed129784SThomas Renninger &cpuinfo_transition_latency.attr, 6511da177e4SLinus Torvalds &scaling_min_freq.attr, 6521da177e4SLinus Torvalds &scaling_max_freq.attr, 6531da177e4SLinus Torvalds &affected_cpus.attr, 654e8628dd0SDarrick J. Wong &related_cpus.attr, 6551da177e4SLinus Torvalds &scaling_governor.attr, 6561da177e4SLinus Torvalds &scaling_driver.attr, 6571da177e4SLinus Torvalds &scaling_available_governors.attr, 6589e76988eSVenki Pallipadi &scaling_setspeed.attr, 6591da177e4SLinus Torvalds NULL 6601da177e4SLinus Torvalds }; 6611da177e4SLinus Torvalds 6621da177e4SLinus Torvalds #define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 6631da177e4SLinus Torvalds #define to_attr(a) container_of(a, struct freq_attr, attr) 6641da177e4SLinus Torvalds 6651da177e4SLinus Torvalds static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 6661da177e4SLinus Torvalds { 6671da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 6681da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 6690db4a8a9SDave Jones ssize_t ret = -EINVAL; 6706eed9404SViresh Kumar 6716eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 6726eed9404SViresh Kumar goto exit; 6735a01f2e8SVenkatesh Pallipadi 6745a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_read(policy->cpu) < 0) 6756eed9404SViresh Kumar goto up_read; 6765a01f2e8SVenkatesh Pallipadi 677e08f5f5bSGautham R Shenoy if (fattr->show) 678e08f5f5bSGautham R Shenoy ret = fattr->show(policy, buf); 679e08f5f5bSGautham R Shenoy else 680e08f5f5bSGautham R Shenoy ret = -EIO; 681e08f5f5bSGautham R Shenoy 6825a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(policy->cpu); 6836eed9404SViresh Kumar 6846eed9404SViresh Kumar up_read: 6856eed9404SViresh Kumar up_read(&cpufreq_rwsem); 6866eed9404SViresh Kumar exit: 6871da177e4SLinus Torvalds return ret; 6881da177e4SLinus Torvalds } 6891da177e4SLinus Torvalds 6901da177e4SLinus Torvalds static ssize_t store(struct kobject *kobj, struct attribute *attr, 6911da177e4SLinus Torvalds const char *buf, size_t count) 6921da177e4SLinus Torvalds { 6931da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 6941da177e4SLinus Torvalds struct freq_attr *fattr = to_attr(attr); 695a07530b4SDave Jones ssize_t ret = -EINVAL; 6966eed9404SViresh Kumar 6976eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 6986eed9404SViresh Kumar goto exit; 6995a01f2e8SVenkatesh Pallipadi 7005a01f2e8SVenkatesh Pallipadi if (lock_policy_rwsem_write(policy->cpu) < 0) 7016eed9404SViresh Kumar goto up_read; 7025a01f2e8SVenkatesh Pallipadi 703e08f5f5bSGautham R Shenoy if (fattr->store) 704e08f5f5bSGautham R Shenoy ret = fattr->store(policy, buf, count); 705e08f5f5bSGautham R Shenoy else 706e08f5f5bSGautham R Shenoy ret = -EIO; 707e08f5f5bSGautham R Shenoy 7085a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 7096eed9404SViresh Kumar 7106eed9404SViresh Kumar up_read: 7116eed9404SViresh Kumar up_read(&cpufreq_rwsem); 7126eed9404SViresh Kumar exit: 7131da177e4SLinus Torvalds return ret; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds 7161da177e4SLinus Torvalds static void cpufreq_sysfs_release(struct kobject *kobj) 7171da177e4SLinus Torvalds { 7181da177e4SLinus Torvalds struct cpufreq_policy *policy = to_policy(kobj); 7192d06d8c4SDominik Brodowski pr_debug("last reference is dropped\n"); 7201da177e4SLinus Torvalds complete(&policy->kobj_unregister); 7211da177e4SLinus Torvalds } 7221da177e4SLinus Torvalds 72352cf25d0SEmese Revfy static const struct sysfs_ops sysfs_ops = { 7241da177e4SLinus Torvalds .show = show, 7251da177e4SLinus Torvalds .store = store, 7261da177e4SLinus Torvalds }; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds static struct kobj_type ktype_cpufreq = { 7291da177e4SLinus Torvalds .sysfs_ops = &sysfs_ops, 7301da177e4SLinus Torvalds .default_attrs = default_attrs, 7311da177e4SLinus Torvalds .release = cpufreq_sysfs_release, 7321da177e4SLinus Torvalds }; 7331da177e4SLinus Torvalds 7342361be23SViresh Kumar struct kobject *cpufreq_global_kobject; 7352361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_global_kobject); 7362361be23SViresh Kumar 7372361be23SViresh Kumar static int cpufreq_global_kobject_usage; 7382361be23SViresh Kumar 7392361be23SViresh Kumar int cpufreq_get_global_kobject(void) 7402361be23SViresh Kumar { 7412361be23SViresh Kumar if (!cpufreq_global_kobject_usage++) 7422361be23SViresh Kumar return kobject_add(cpufreq_global_kobject, 7432361be23SViresh Kumar &cpu_subsys.dev_root->kobj, "%s", "cpufreq"); 7442361be23SViresh Kumar 7452361be23SViresh Kumar return 0; 7462361be23SViresh Kumar } 7472361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_get_global_kobject); 7482361be23SViresh Kumar 7492361be23SViresh Kumar void cpufreq_put_global_kobject(void) 7502361be23SViresh Kumar { 7512361be23SViresh Kumar if (!--cpufreq_global_kobject_usage) 7522361be23SViresh Kumar kobject_del(cpufreq_global_kobject); 7532361be23SViresh Kumar } 7542361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_put_global_kobject); 7552361be23SViresh Kumar 7562361be23SViresh Kumar int cpufreq_sysfs_create_file(const struct attribute *attr) 7572361be23SViresh Kumar { 7582361be23SViresh Kumar int ret = cpufreq_get_global_kobject(); 7592361be23SViresh Kumar 7602361be23SViresh Kumar if (!ret) { 7612361be23SViresh Kumar ret = sysfs_create_file(cpufreq_global_kobject, attr); 7622361be23SViresh Kumar if (ret) 7632361be23SViresh Kumar cpufreq_put_global_kobject(); 7642361be23SViresh Kumar } 7652361be23SViresh Kumar 7662361be23SViresh Kumar return ret; 7672361be23SViresh Kumar } 7682361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_create_file); 7692361be23SViresh Kumar 7702361be23SViresh Kumar void cpufreq_sysfs_remove_file(const struct attribute *attr) 7712361be23SViresh Kumar { 7722361be23SViresh Kumar sysfs_remove_file(cpufreq_global_kobject, attr); 7732361be23SViresh Kumar cpufreq_put_global_kobject(); 7742361be23SViresh Kumar } 7752361be23SViresh Kumar EXPORT_SYMBOL(cpufreq_sysfs_remove_file); 7762361be23SViresh Kumar 77719d6f7ecSDave Jones /* symlink affected CPUs */ 778308b60e7SViresh Kumar static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy) 77919d6f7ecSDave Jones { 78019d6f7ecSDave Jones unsigned int j; 78119d6f7ecSDave Jones int ret = 0; 78219d6f7ecSDave Jones 78319d6f7ecSDave Jones for_each_cpu(j, policy->cpus) { 7848a25a2fdSKay Sievers struct device *cpu_dev; 78519d6f7ecSDave Jones 786308b60e7SViresh Kumar if (j == policy->cpu) 78719d6f7ecSDave Jones continue; 78819d6f7ecSDave Jones 789e8fdde10SViresh Kumar pr_debug("Adding link for CPU: %u\n", j); 7908a25a2fdSKay Sievers cpu_dev = get_cpu_device(j); 7918a25a2fdSKay Sievers ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 79219d6f7ecSDave Jones "cpufreq"); 79371c3461eSRafael J. Wysocki if (ret) 79471c3461eSRafael J. Wysocki break; 79519d6f7ecSDave Jones } 79619d6f7ecSDave Jones return ret; 79719d6f7ecSDave Jones } 79819d6f7ecSDave Jones 799308b60e7SViresh Kumar static int cpufreq_add_dev_interface(struct cpufreq_policy *policy, 8008a25a2fdSKay Sievers struct device *dev) 801909a694eSDave Jones { 802909a694eSDave Jones struct freq_attr **drv_attr; 803909a694eSDave Jones int ret = 0; 804909a694eSDave Jones 805909a694eSDave Jones /* prepare interface data */ 806909a694eSDave Jones ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 8078a25a2fdSKay Sievers &dev->kobj, "cpufreq"); 808909a694eSDave Jones if (ret) 809909a694eSDave Jones return ret; 810909a694eSDave Jones 811909a694eSDave Jones /* set up files for this cpu device */ 8121c3d85ddSRafael J. Wysocki drv_attr = cpufreq_driver->attr; 813909a694eSDave Jones while ((drv_attr) && (*drv_attr)) { 814909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 815909a694eSDave Jones if (ret) 8161c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 817909a694eSDave Jones drv_attr++; 818909a694eSDave Jones } 8191c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 820909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 821909a694eSDave Jones if (ret) 8221c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 823909a694eSDave Jones } 8241c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) { 825909a694eSDave Jones ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 826909a694eSDave Jones if (ret) 8271c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 828909a694eSDave Jones } 8291c3d85ddSRafael J. Wysocki if (cpufreq_driver->bios_limit) { 830e2f74f35SThomas Renninger ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 831e2f74f35SThomas Renninger if (ret) 8321c3d85ddSRafael J. Wysocki goto err_out_kobj_put; 833e2f74f35SThomas Renninger } 834909a694eSDave Jones 835308b60e7SViresh Kumar ret = cpufreq_add_dev_symlink(policy); 836ecf7e461SDave Jones if (ret) 837ecf7e461SDave Jones goto err_out_kobj_put; 838ecf7e461SDave Jones 839e18f1682SSrivatsa S. Bhat return ret; 840e18f1682SSrivatsa S. Bhat 841e18f1682SSrivatsa S. Bhat err_out_kobj_put: 842e18f1682SSrivatsa S. Bhat kobject_put(&policy->kobj); 843e18f1682SSrivatsa S. Bhat wait_for_completion(&policy->kobj_unregister); 844e18f1682SSrivatsa S. Bhat return ret; 845e18f1682SSrivatsa S. Bhat } 846e18f1682SSrivatsa S. Bhat 847e18f1682SSrivatsa S. Bhat static void cpufreq_init_policy(struct cpufreq_policy *policy) 848e18f1682SSrivatsa S. Bhat { 849e18f1682SSrivatsa S. Bhat struct cpufreq_policy new_policy; 850e18f1682SSrivatsa S. Bhat int ret = 0; 851e18f1682SSrivatsa S. Bhat 852d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 853ecf7e461SDave Jones /* assure that the starting sequence is run in __cpufreq_set_policy */ 854ecf7e461SDave Jones policy->governor = NULL; 855ecf7e461SDave Jones 856ecf7e461SDave Jones /* set default policy */ 857ecf7e461SDave Jones ret = __cpufreq_set_policy(policy, &new_policy); 858ecf7e461SDave Jones policy->user_policy.policy = policy->policy; 859ecf7e461SDave Jones policy->user_policy.governor = policy->governor; 860ecf7e461SDave Jones 861ecf7e461SDave Jones if (ret) { 8622d06d8c4SDominik Brodowski pr_debug("setting policy failed\n"); 8631c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 8641c3d85ddSRafael J. Wysocki cpufreq_driver->exit(policy); 865ecf7e461SDave Jones } 866909a694eSDave Jones } 867909a694eSDave Jones 868fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 869d8d3b471SViresh Kumar static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, 870d8d3b471SViresh Kumar unsigned int cpu, struct device *dev, 871d8d3b471SViresh Kumar bool frozen) 872fcf80582SViresh Kumar { 8731c3d85ddSRafael J. Wysocki int ret = 0, has_target = !!cpufreq_driver->target; 874fcf80582SViresh Kumar unsigned long flags; 875fcf80582SViresh Kumar 8763de9bdebSViresh Kumar if (has_target) { 8773de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 8783de9bdebSViresh Kumar if (ret) { 8793de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 8803de9bdebSViresh Kumar return ret; 8813de9bdebSViresh Kumar } 8823de9bdebSViresh Kumar } 883fcf80582SViresh Kumar 884d8d3b471SViresh Kumar lock_policy_rwsem_write(policy->cpu); 8852eaa3e2dSViresh Kumar 8860d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 8872eaa3e2dSViresh Kumar 888fcf80582SViresh Kumar cpumask_set_cpu(cpu, policy->cpus); 889fcf80582SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = policy; 8900d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 891fcf80582SViresh Kumar 892d8d3b471SViresh Kumar unlock_policy_rwsem_write(policy->cpu); 8932eaa3e2dSViresh Kumar 894820c6ca2SViresh Kumar if (has_target) { 8953de9bdebSViresh Kumar if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) || 8963de9bdebSViresh Kumar (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) { 8973de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", __func__); 8983de9bdebSViresh Kumar return ret; 8993de9bdebSViresh Kumar } 900820c6ca2SViresh Kumar } 901fcf80582SViresh Kumar 902a82fab29SSrivatsa S. Bhat /* Don't touch sysfs links during light-weight init */ 90371c3461eSRafael J. Wysocki if (!frozen) 904a82fab29SSrivatsa S. Bhat ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"); 905a82fab29SSrivatsa S. Bhat 906a82fab29SSrivatsa S. Bhat return ret; 907fcf80582SViresh Kumar } 908fcf80582SViresh Kumar #endif 9091da177e4SLinus Torvalds 9108414809cSSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu) 9118414809cSSrivatsa S. Bhat { 9128414809cSSrivatsa S. Bhat struct cpufreq_policy *policy; 9138414809cSSrivatsa S. Bhat unsigned long flags; 9148414809cSSrivatsa S. Bhat 9158414809cSSrivatsa S. Bhat write_lock_irqsave(&cpufreq_driver_lock, flags); 9168414809cSSrivatsa S. Bhat 9178414809cSSrivatsa S. Bhat policy = per_cpu(cpufreq_cpu_data_fallback, cpu); 9188414809cSSrivatsa S. Bhat 9198414809cSSrivatsa S. Bhat write_unlock_irqrestore(&cpufreq_driver_lock, flags); 9208414809cSSrivatsa S. Bhat 9218414809cSSrivatsa S. Bhat return policy; 9228414809cSSrivatsa S. Bhat } 9238414809cSSrivatsa S. Bhat 924e9698cc5SSrivatsa S. Bhat static struct cpufreq_policy *cpufreq_policy_alloc(void) 925e9698cc5SSrivatsa S. Bhat { 926e9698cc5SSrivatsa S. Bhat struct cpufreq_policy *policy; 927e9698cc5SSrivatsa S. Bhat 928e9698cc5SSrivatsa S. Bhat policy = kzalloc(sizeof(*policy), GFP_KERNEL); 929e9698cc5SSrivatsa S. Bhat if (!policy) 930e9698cc5SSrivatsa S. Bhat return NULL; 931e9698cc5SSrivatsa S. Bhat 932e9698cc5SSrivatsa S. Bhat if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 933e9698cc5SSrivatsa S. Bhat goto err_free_policy; 934e9698cc5SSrivatsa S. Bhat 935e9698cc5SSrivatsa S. Bhat if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 936e9698cc5SSrivatsa S. Bhat goto err_free_cpumask; 937e9698cc5SSrivatsa S. Bhat 938c88a1f8bSLukasz Majewski INIT_LIST_HEAD(&policy->policy_list); 939e9698cc5SSrivatsa S. Bhat return policy; 940e9698cc5SSrivatsa S. Bhat 941e9698cc5SSrivatsa S. Bhat err_free_cpumask: 942e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 943e9698cc5SSrivatsa S. Bhat err_free_policy: 944e9698cc5SSrivatsa S. Bhat kfree(policy); 945e9698cc5SSrivatsa S. Bhat 946e9698cc5SSrivatsa S. Bhat return NULL; 947e9698cc5SSrivatsa S. Bhat } 948e9698cc5SSrivatsa S. Bhat 949e9698cc5SSrivatsa S. Bhat static void cpufreq_policy_free(struct cpufreq_policy *policy) 950e9698cc5SSrivatsa S. Bhat { 951e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->related_cpus); 952e9698cc5SSrivatsa S. Bhat free_cpumask_var(policy->cpus); 953e9698cc5SSrivatsa S. Bhat kfree(policy); 954e9698cc5SSrivatsa S. Bhat } 955e9698cc5SSrivatsa S. Bhat 956a82fab29SSrivatsa S. Bhat static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif, 957a82fab29SSrivatsa S. Bhat bool frozen) 9581da177e4SLinus Torvalds { 959fcf80582SViresh Kumar unsigned int j, cpu = dev->id; 96065922465SViresh Kumar int ret = -ENOMEM; 9611da177e4SLinus Torvalds struct cpufreq_policy *policy; 9621da177e4SLinus Torvalds unsigned long flags; 96390e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU 964fcf80582SViresh Kumar struct cpufreq_governor *gov; 965878f6e07SRafael J. Wysocki int sibling; 96690e41bacSPrarit Bhargava #endif 9671da177e4SLinus Torvalds 968c32b6b8eSAshok Raj if (cpu_is_offline(cpu)) 969c32b6b8eSAshok Raj return 0; 970c32b6b8eSAshok Raj 9712d06d8c4SDominik Brodowski pr_debug("adding CPU %u\n", cpu); 9721da177e4SLinus Torvalds 9731da177e4SLinus Torvalds #ifdef CONFIG_SMP 9741da177e4SLinus Torvalds /* check whether a different CPU already registered this 9751da177e4SLinus Torvalds * CPU because it is in the same boat. */ 9761da177e4SLinus Torvalds policy = cpufreq_cpu_get(cpu); 9771da177e4SLinus Torvalds if (unlikely(policy)) { 9788ff69732SDave Jones cpufreq_cpu_put(policy); 9791da177e4SLinus Torvalds return 0; 9801da177e4SLinus Torvalds } 981fcf80582SViresh Kumar 9826eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 9836eed9404SViresh Kumar return 0; 9846eed9404SViresh Kumar 985fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 986fcf80582SViresh Kumar /* Check if this cpu was hot-unplugged earlier and has siblings */ 9870d1857a1SNathan Zimmer read_lock_irqsave(&cpufreq_driver_lock, flags); 988878f6e07SRafael J. Wysocki for_each_online_cpu(sibling) { 989878f6e07SRafael J. Wysocki struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling); 990878f6e07SRafael J. Wysocki if (cp && cpumask_test_cpu(cpu, cp->related_cpus)) { 9910d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 992878f6e07SRafael J. Wysocki ret = cpufreq_add_policy_cpu(cp, cpu, dev, frozen); 9936eed9404SViresh Kumar up_read(&cpufreq_rwsem); 9946eed9404SViresh Kumar return ret; 995fcf80582SViresh Kumar } 9962eaa3e2dSViresh Kumar } 9970d1857a1SNathan Zimmer read_unlock_irqrestore(&cpufreq_driver_lock, flags); 998fcf80582SViresh Kumar #endif 9991da177e4SLinus Torvalds #endif 10001da177e4SLinus Torvalds 10018414809cSSrivatsa S. Bhat if (frozen) 10028414809cSSrivatsa S. Bhat /* Restore the saved policy when doing light-weight init */ 10038414809cSSrivatsa S. Bhat policy = cpufreq_policy_restore(cpu); 10048414809cSSrivatsa S. Bhat else 1005e9698cc5SSrivatsa S. Bhat policy = cpufreq_policy_alloc(); 10068414809cSSrivatsa S. Bhat 1007059019a3SDave Jones if (!policy) 10081da177e4SLinus Torvalds goto nomem_out; 1009059019a3SDave Jones 10101da177e4SLinus Torvalds policy->cpu = cpu; 101165922465SViresh Kumar policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 1012835481d9SRusty Russell cpumask_copy(policy->cpus, cpumask_of(cpu)); 10131da177e4SLinus Torvalds 10141da177e4SLinus Torvalds init_completion(&policy->kobj_unregister); 101565f27f38SDavid Howells INIT_WORK(&policy->update, handle_update); 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds /* call driver. From then on the cpufreq must be able 10181da177e4SLinus Torvalds * to accept all calls to ->verify and ->setpolicy for this CPU 10191da177e4SLinus Torvalds */ 10201c3d85ddSRafael J. Wysocki ret = cpufreq_driver->init(policy); 10211da177e4SLinus Torvalds if (ret) { 10222d06d8c4SDominik Brodowski pr_debug("initialization failed\n"); 10232eaa3e2dSViresh Kumar goto err_set_policy_cpu; 10241da177e4SLinus Torvalds } 1025643ae6e8SViresh Kumar 1026fcf80582SViresh Kumar /* related cpus should atleast have policy->cpus */ 1027fcf80582SViresh Kumar cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 1028fcf80582SViresh Kumar 1029643ae6e8SViresh Kumar /* 1030643ae6e8SViresh Kumar * affected cpus must always be the one, which are online. We aren't 1031643ae6e8SViresh Kumar * managing offline cpus here. 1032643ae6e8SViresh Kumar */ 1033643ae6e8SViresh Kumar cpumask_and(policy->cpus, policy->cpus, cpu_online_mask); 1034643ae6e8SViresh Kumar 1035187d9f4eSMike Chan policy->user_policy.min = policy->min; 1036187d9f4eSMike Chan policy->user_policy.max = policy->max; 10371da177e4SLinus Torvalds 1038a1531acdSThomas Renninger blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1039a1531acdSThomas Renninger CPUFREQ_START, policy); 1040a1531acdSThomas Renninger 1041fcf80582SViresh Kumar #ifdef CONFIG_HOTPLUG_CPU 1042fcf80582SViresh Kumar gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu)); 1043fcf80582SViresh Kumar if (gov) { 1044fcf80582SViresh Kumar policy->governor = gov; 1045fcf80582SViresh Kumar pr_debug("Restoring governor %s for cpu %d\n", 1046fcf80582SViresh Kumar policy->governor->name, cpu); 10474bfa042cSThomas Renninger } 1048fcf80582SViresh Kumar #endif 10491da177e4SLinus Torvalds 1050e18f1682SSrivatsa S. Bhat write_lock_irqsave(&cpufreq_driver_lock, flags); 1051*474deff7SViresh Kumar for_each_cpu(j, policy->cpus) 1052e18f1682SSrivatsa S. Bhat per_cpu(cpufreq_cpu_data, j) = policy; 1053e18f1682SSrivatsa S. Bhat write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1054e18f1682SSrivatsa S. Bhat 1055a82fab29SSrivatsa S. Bhat if (!frozen) { 1056308b60e7SViresh Kumar ret = cpufreq_add_dev_interface(policy, dev); 105719d6f7ecSDave Jones if (ret) 10580142f9dcSAhmed S. Darwish goto err_out_unregister; 10599515f4d6SViresh Kumar } 1060c88a1f8bSLukasz Majewski 1061c88a1f8bSLukasz Majewski write_lock_irqsave(&cpufreq_driver_lock, flags); 1062c88a1f8bSLukasz Majewski list_add(&policy->policy_list, &cpufreq_policy_list); 1063c88a1f8bSLukasz Majewski write_unlock_irqrestore(&cpufreq_driver_lock, flags); 10648ff69732SDave Jones 1065e18f1682SSrivatsa S. Bhat cpufreq_init_policy(policy); 1066e18f1682SSrivatsa S. Bhat 1067038c5b3eSGreg Kroah-Hartman kobject_uevent(&policy->kobj, KOBJ_ADD); 10686eed9404SViresh Kumar up_read(&cpufreq_rwsem); 10696eed9404SViresh Kumar 10702d06d8c4SDominik Brodowski pr_debug("initialization complete\n"); 10711da177e4SLinus Torvalds 10721da177e4SLinus Torvalds return 0; 10731da177e4SLinus Torvalds 10741da177e4SLinus Torvalds err_out_unregister: 10750d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 1076*474deff7SViresh Kumar for_each_cpu(j, policy->cpus) 10777a6aedfaSMike Travis per_cpu(cpufreq_cpu_data, j) = NULL; 10780d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 10791da177e4SLinus Torvalds 10802eaa3e2dSViresh Kumar err_set_policy_cpu: 1081e9698cc5SSrivatsa S. Bhat cpufreq_policy_free(policy); 10821da177e4SLinus Torvalds nomem_out: 10836eed9404SViresh Kumar up_read(&cpufreq_rwsem); 10846eed9404SViresh Kumar 10851da177e4SLinus Torvalds return ret; 10861da177e4SLinus Torvalds } 10871da177e4SLinus Torvalds 1088a82fab29SSrivatsa S. Bhat /** 1089a82fab29SSrivatsa S. Bhat * cpufreq_add_dev - add a CPU device 1090a82fab29SSrivatsa S. Bhat * 1091a82fab29SSrivatsa S. Bhat * Adds the cpufreq interface for a CPU device. 1092a82fab29SSrivatsa S. Bhat * 1093a82fab29SSrivatsa S. Bhat * The Oracle says: try running cpufreq registration/unregistration concurrently 1094a82fab29SSrivatsa S. Bhat * with with cpu hotplugging and all hell will break loose. Tried to clean this 1095a82fab29SSrivatsa S. Bhat * mess up, but more thorough testing is needed. - Mathieu 1096a82fab29SSrivatsa S. Bhat */ 1097a82fab29SSrivatsa S. Bhat static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif) 1098a82fab29SSrivatsa S. Bhat { 1099a82fab29SSrivatsa S. Bhat return __cpufreq_add_dev(dev, sif, false); 1100a82fab29SSrivatsa S. Bhat } 1101a82fab29SSrivatsa S. Bhat 1102b8eed8afSViresh Kumar static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu) 1103b8eed8afSViresh Kumar { 1104b8eed8afSViresh Kumar policy->last_cpu = policy->cpu; 1105b8eed8afSViresh Kumar policy->cpu = cpu; 1106b8eed8afSViresh Kumar 1107b8eed8afSViresh Kumar #ifdef CONFIG_CPU_FREQ_TABLE 1108b8eed8afSViresh Kumar cpufreq_frequency_table_update_policy_cpu(policy); 1109b8eed8afSViresh Kumar #endif 1110b8eed8afSViresh Kumar blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1111b8eed8afSViresh Kumar CPUFREQ_UPDATE_POLICY_CPU, policy); 1112b8eed8afSViresh Kumar } 11131da177e4SLinus Torvalds 11143a3e9e06SViresh Kumar static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy, 1115a82fab29SSrivatsa S. Bhat unsigned int old_cpu, bool frozen) 1116f9ba680dSSrivatsa S. Bhat { 1117f9ba680dSSrivatsa S. Bhat struct device *cpu_dev; 1118f9ba680dSSrivatsa S. Bhat int ret; 1119f9ba680dSSrivatsa S. Bhat 1120f9ba680dSSrivatsa S. Bhat /* first sibling now owns the new sysfs dir */ 11213a3e9e06SViresh Kumar cpu_dev = get_cpu_device(cpumask_first(policy->cpus)); 1122a82fab29SSrivatsa S. Bhat 1123a82fab29SSrivatsa S. Bhat /* Don't touch sysfs files during light-weight tear-down */ 1124a82fab29SSrivatsa S. Bhat if (frozen) 1125a82fab29SSrivatsa S. Bhat return cpu_dev->id; 1126a82fab29SSrivatsa S. Bhat 1127f9ba680dSSrivatsa S. Bhat sysfs_remove_link(&cpu_dev->kobj, "cpufreq"); 11283a3e9e06SViresh Kumar ret = kobject_move(&policy->kobj, &cpu_dev->kobj); 1129f9ba680dSSrivatsa S. Bhat if (ret) { 1130f9ba680dSSrivatsa S. Bhat pr_err("%s: Failed to move kobj: %d", __func__, ret); 1131f9ba680dSSrivatsa S. Bhat 1132f9ba680dSSrivatsa S. Bhat WARN_ON(lock_policy_rwsem_write(old_cpu)); 11333a3e9e06SViresh Kumar cpumask_set_cpu(old_cpu, policy->cpus); 1134f9ba680dSSrivatsa S. Bhat unlock_policy_rwsem_write(old_cpu); 1135f9ba680dSSrivatsa S. Bhat 11363a3e9e06SViresh Kumar ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj, 1137f9ba680dSSrivatsa S. Bhat "cpufreq"); 1138f9ba680dSSrivatsa S. Bhat 1139f9ba680dSSrivatsa S. Bhat return -EINVAL; 1140f9ba680dSSrivatsa S. Bhat } 1141f9ba680dSSrivatsa S. Bhat 1142f9ba680dSSrivatsa S. Bhat return cpu_dev->id; 1143f9ba680dSSrivatsa S. Bhat } 1144f9ba680dSSrivatsa S. Bhat 11451da177e4SLinus Torvalds /** 11465a01f2e8SVenkatesh Pallipadi * __cpufreq_remove_dev - remove a CPU device 11471da177e4SLinus Torvalds * 11481da177e4SLinus Torvalds * Removes the cpufreq interface for a CPU device. 11495a01f2e8SVenkatesh Pallipadi * Caller should already have policy_rwsem in write mode for this CPU. 11505a01f2e8SVenkatesh Pallipadi * This routine frees the rwsem before returning. 11511da177e4SLinus Torvalds */ 1152bb176f7dSViresh Kumar static int __cpufreq_remove_dev(struct device *dev, 1153a82fab29SSrivatsa S. Bhat struct subsys_interface *sif, bool frozen) 11541da177e4SLinus Torvalds { 1155f9ba680dSSrivatsa S. Bhat unsigned int cpu = dev->id, cpus; 11563de9bdebSViresh Kumar int new_cpu, ret; 11571da177e4SLinus Torvalds unsigned long flags; 11583a3e9e06SViresh Kumar struct cpufreq_policy *policy; 1159499bca9bSAmerigo Wang struct kobject *kobj; 1160499bca9bSAmerigo Wang struct completion *cmp; 11611da177e4SLinus Torvalds 1162b8eed8afSViresh Kumar pr_debug("%s: unregistering CPU %u\n", __func__, cpu); 11631da177e4SLinus Torvalds 11640d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 11651da177e4SLinus Torvalds 11663a3e9e06SViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 11671da177e4SLinus Torvalds 11688414809cSSrivatsa S. Bhat /* Save the policy somewhere when doing a light-weight tear-down */ 11698414809cSSrivatsa S. Bhat if (frozen) 11703a3e9e06SViresh Kumar per_cpu(cpufreq_cpu_data_fallback, cpu) = policy; 11718414809cSSrivatsa S. Bhat 11720d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 11731da177e4SLinus Torvalds 11743a3e9e06SViresh Kumar if (!policy) { 1175b8eed8afSViresh Kumar pr_debug("%s: No cpu_data found\n", __func__); 11761da177e4SLinus Torvalds return -EINVAL; 11771da177e4SLinus Torvalds } 11781da177e4SLinus Torvalds 11793de9bdebSViresh Kumar if (cpufreq_driver->target) { 11803de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 11813de9bdebSViresh Kumar if (ret) { 11823de9bdebSViresh Kumar pr_err("%s: Failed to stop governor\n", __func__); 11833de9bdebSViresh Kumar return ret; 11843de9bdebSViresh Kumar } 11853de9bdebSViresh Kumar } 11865a01f2e8SVenkatesh Pallipadi 11871da177e4SLinus Torvalds #ifdef CONFIG_HOTPLUG_CPU 11881c3d85ddSRafael J. Wysocki if (!cpufreq_driver->setpolicy) 1189fa69e33fSDirk Brandewie strncpy(per_cpu(cpufreq_cpu_governor, cpu), 11903a3e9e06SViresh Kumar policy->governor->name, CPUFREQ_NAME_LEN); 11911da177e4SLinus Torvalds #endif 11921da177e4SLinus Torvalds 11932eaa3e2dSViresh Kumar WARN_ON(lock_policy_rwsem_write(cpu)); 11943a3e9e06SViresh Kumar cpus = cpumask_weight(policy->cpus); 1195e4969ebaSViresh Kumar 1196e4969ebaSViresh Kumar if (cpus > 1) 11973a3e9e06SViresh Kumar cpumask_clear_cpu(cpu, policy->cpus); 11982eaa3e2dSViresh Kumar unlock_policy_rwsem_write(cpu); 11991da177e4SLinus Torvalds 12003a3e9e06SViresh Kumar if (cpu != policy->cpu && !frozen) { 120173bf0fc2SViresh Kumar sysfs_remove_link(&dev->kobj, "cpufreq"); 120273bf0fc2SViresh Kumar } else if (cpus > 1) { 12032eaa3e2dSViresh Kumar 12043a3e9e06SViresh Kumar new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen); 1205f9ba680dSSrivatsa S. Bhat if (new_cpu >= 0) { 12062eaa3e2dSViresh Kumar WARN_ON(lock_policy_rwsem_write(cpu)); 12073a3e9e06SViresh Kumar update_policy_cpu(policy, new_cpu); 12082eaa3e2dSViresh Kumar unlock_policy_rwsem_write(cpu); 1209a82fab29SSrivatsa S. Bhat 1210a82fab29SSrivatsa S. Bhat if (!frozen) { 1211f9ba680dSSrivatsa S. Bhat pr_debug("%s: policy Kobject moved to cpu: %d " 1212f9ba680dSSrivatsa S. Bhat "from: %d\n",__func__, new_cpu, cpu); 12131da177e4SLinus Torvalds } 12141da177e4SLinus Torvalds } 1215a82fab29SSrivatsa S. Bhat } 1216b8eed8afSViresh Kumar 1217b8eed8afSViresh Kumar /* If cpu is last user of policy, free policy */ 1218b8eed8afSViresh Kumar if (cpus == 1) { 12193de9bdebSViresh Kumar if (cpufreq_driver->target) { 12203de9bdebSViresh Kumar ret = __cpufreq_governor(policy, 12213de9bdebSViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 12223de9bdebSViresh Kumar if (ret) { 12233de9bdebSViresh Kumar pr_err("%s: Failed to exit governor\n", 12243de9bdebSViresh Kumar __func__); 12253de9bdebSViresh Kumar return ret; 12263de9bdebSViresh Kumar } 12273de9bdebSViresh Kumar } 12282a998599SRafael J. Wysocki 12298414809cSSrivatsa S. Bhat if (!frozen) { 12302eaa3e2dSViresh Kumar lock_policy_rwsem_read(cpu); 12313a3e9e06SViresh Kumar kobj = &policy->kobj; 12323a3e9e06SViresh Kumar cmp = &policy->kobj_unregister; 12332eaa3e2dSViresh Kumar unlock_policy_rwsem_read(cpu); 1234499bca9bSAmerigo Wang kobject_put(kobj); 12351da177e4SLinus Torvalds 12368414809cSSrivatsa S. Bhat /* 12378414809cSSrivatsa S. Bhat * We need to make sure that the underlying kobj is 12388414809cSSrivatsa S. Bhat * actually not referenced anymore by anybody before we 12398414809cSSrivatsa S. Bhat * proceed with unloading. 12401da177e4SLinus Torvalds */ 12412d06d8c4SDominik Brodowski pr_debug("waiting for dropping of refcount\n"); 1242499bca9bSAmerigo Wang wait_for_completion(cmp); 12432d06d8c4SDominik Brodowski pr_debug("wait complete\n"); 12448414809cSSrivatsa S. Bhat } 12451da177e4SLinus Torvalds 12468414809cSSrivatsa S. Bhat /* 12478414809cSSrivatsa S. Bhat * Perform the ->exit() even during light-weight tear-down, 12488414809cSSrivatsa S. Bhat * since this is a core component, and is essential for the 12498414809cSSrivatsa S. Bhat * subsequent light-weight ->init() to succeed. 12508414809cSSrivatsa S. Bhat */ 12511c3d85ddSRafael J. Wysocki if (cpufreq_driver->exit) 12523a3e9e06SViresh Kumar cpufreq_driver->exit(policy); 125327ecddc2SJacob Shin 12549515f4d6SViresh Kumar /* Remove policy from list of active policies */ 12559515f4d6SViresh Kumar write_lock_irqsave(&cpufreq_driver_lock, flags); 12569515f4d6SViresh Kumar list_del(&policy->policy_list); 12579515f4d6SViresh Kumar write_unlock_irqrestore(&cpufreq_driver_lock, flags); 12589515f4d6SViresh Kumar 12598414809cSSrivatsa S. Bhat if (!frozen) 12603a3e9e06SViresh Kumar cpufreq_policy_free(policy); 12612a998599SRafael J. Wysocki } else { 12622a998599SRafael J. Wysocki if (cpufreq_driver->target) { 12633de9bdebSViresh Kumar if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) || 12643de9bdebSViresh Kumar (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) { 12653de9bdebSViresh Kumar pr_err("%s: Failed to start governor\n", 12663de9bdebSViresh Kumar __func__); 12673de9bdebSViresh Kumar return ret; 12683de9bdebSViresh Kumar } 1269b8eed8afSViresh Kumar } 12702a998599SRafael J. Wysocki } 12711da177e4SLinus Torvalds 1272*474deff7SViresh Kumar per_cpu(cpufreq_cpu_data, cpu) = NULL; 12731da177e4SLinus Torvalds return 0; 12741da177e4SLinus Torvalds } 12751da177e4SLinus Torvalds 12768a25a2fdSKay Sievers static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif) 12775a01f2e8SVenkatesh Pallipadi { 12788a25a2fdSKay Sievers unsigned int cpu = dev->id; 12795a01f2e8SVenkatesh Pallipadi int retval; 1280ec28297aSVenki Pallipadi 1281ec28297aSVenki Pallipadi if (cpu_is_offline(cpu)) 1282ec28297aSVenki Pallipadi return 0; 1283ec28297aSVenki Pallipadi 1284a82fab29SSrivatsa S. Bhat retval = __cpufreq_remove_dev(dev, sif, false); 12855a01f2e8SVenkatesh Pallipadi return retval; 12865a01f2e8SVenkatesh Pallipadi } 12875a01f2e8SVenkatesh Pallipadi 128865f27f38SDavid Howells static void handle_update(struct work_struct *work) 12891da177e4SLinus Torvalds { 129065f27f38SDavid Howells struct cpufreq_policy *policy = 129165f27f38SDavid Howells container_of(work, struct cpufreq_policy, update); 129265f27f38SDavid Howells unsigned int cpu = policy->cpu; 12932d06d8c4SDominik Brodowski pr_debug("handle_update for cpu %u called\n", cpu); 12941da177e4SLinus Torvalds cpufreq_update_policy(cpu); 12951da177e4SLinus Torvalds } 12961da177e4SLinus Torvalds 12971da177e4SLinus Torvalds /** 1298bb176f7dSViresh Kumar * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're 1299bb176f7dSViresh Kumar * in deep trouble. 13001da177e4SLinus Torvalds * @cpu: cpu number 13011da177e4SLinus Torvalds * @old_freq: CPU frequency the kernel thinks the CPU runs at 13021da177e4SLinus Torvalds * @new_freq: CPU frequency the CPU actually runs at 13031da177e4SLinus Torvalds * 130429464f28SDave Jones * We adjust to current frequency first, and need to clean up later. 130529464f28SDave Jones * So either call to cpufreq_update_policy() or schedule handle_update()). 13061da177e4SLinus Torvalds */ 1307e08f5f5bSGautham R Shenoy static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1308e08f5f5bSGautham R Shenoy unsigned int new_freq) 13091da177e4SLinus Torvalds { 1310b43a7ffbSViresh Kumar struct cpufreq_policy *policy; 13111da177e4SLinus Torvalds struct cpufreq_freqs freqs; 1312b43a7ffbSViresh Kumar unsigned long flags; 1313b43a7ffbSViresh Kumar 13142d06d8c4SDominik Brodowski pr_debug("Warning: CPU frequency out of sync: cpufreq and timing " 13151da177e4SLinus Torvalds "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 13161da177e4SLinus Torvalds 13171da177e4SLinus Torvalds freqs.old = old_freq; 13181da177e4SLinus Torvalds freqs.new = new_freq; 1319b43a7ffbSViresh Kumar 1320b43a7ffbSViresh Kumar read_lock_irqsave(&cpufreq_driver_lock, flags); 1321b43a7ffbSViresh Kumar policy = per_cpu(cpufreq_cpu_data, cpu); 1322b43a7ffbSViresh Kumar read_unlock_irqrestore(&cpufreq_driver_lock, flags); 1323b43a7ffbSViresh Kumar 1324b43a7ffbSViresh Kumar cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE); 1325b43a7ffbSViresh Kumar cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE); 13261da177e4SLinus Torvalds } 13271da177e4SLinus Torvalds 13281da177e4SLinus Torvalds /** 13294ab70df4SDhaval Giani * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 133095235ca2SVenkatesh Pallipadi * @cpu: CPU number 133195235ca2SVenkatesh Pallipadi * 133295235ca2SVenkatesh Pallipadi * This is the last known freq, without actually getting it from the driver. 133395235ca2SVenkatesh Pallipadi * Return value will be same as what is shown in scaling_cur_freq in sysfs. 133495235ca2SVenkatesh Pallipadi */ 133595235ca2SVenkatesh Pallipadi unsigned int cpufreq_quick_get(unsigned int cpu) 133695235ca2SVenkatesh Pallipadi { 13379e21ba8bSDirk Brandewie struct cpufreq_policy *policy; 1338e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 133995235ca2SVenkatesh Pallipadi 13401c3d85ddSRafael J. Wysocki if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get) 13411c3d85ddSRafael J. Wysocki return cpufreq_driver->get(cpu); 13429e21ba8bSDirk Brandewie 13439e21ba8bSDirk Brandewie policy = cpufreq_cpu_get(cpu); 134495235ca2SVenkatesh Pallipadi if (policy) { 1345e08f5f5bSGautham R Shenoy ret_freq = policy->cur; 134695235ca2SVenkatesh Pallipadi cpufreq_cpu_put(policy); 134795235ca2SVenkatesh Pallipadi } 134895235ca2SVenkatesh Pallipadi 13494d34a67dSDave Jones return ret_freq; 135095235ca2SVenkatesh Pallipadi } 135195235ca2SVenkatesh Pallipadi EXPORT_SYMBOL(cpufreq_quick_get); 135295235ca2SVenkatesh Pallipadi 13533d737108SJesse Barnes /** 13543d737108SJesse Barnes * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU 13553d737108SJesse Barnes * @cpu: CPU number 13563d737108SJesse Barnes * 13573d737108SJesse Barnes * Just return the max possible frequency for a given CPU. 13583d737108SJesse Barnes */ 13593d737108SJesse Barnes unsigned int cpufreq_quick_get_max(unsigned int cpu) 13603d737108SJesse Barnes { 13613d737108SJesse Barnes struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 13623d737108SJesse Barnes unsigned int ret_freq = 0; 13633d737108SJesse Barnes 13643d737108SJesse Barnes if (policy) { 13653d737108SJesse Barnes ret_freq = policy->max; 13663d737108SJesse Barnes cpufreq_cpu_put(policy); 13673d737108SJesse Barnes } 13683d737108SJesse Barnes 13693d737108SJesse Barnes return ret_freq; 13703d737108SJesse Barnes } 13713d737108SJesse Barnes EXPORT_SYMBOL(cpufreq_quick_get_max); 13723d737108SJesse Barnes 13735a01f2e8SVenkatesh Pallipadi static unsigned int __cpufreq_get(unsigned int cpu) 13741da177e4SLinus Torvalds { 13757a6aedfaSMike Travis struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1376e08f5f5bSGautham R Shenoy unsigned int ret_freq = 0; 13771da177e4SLinus Torvalds 13781c3d85ddSRafael J. Wysocki if (!cpufreq_driver->get) 13794d34a67dSDave Jones return ret_freq; 13801da177e4SLinus Torvalds 13811c3d85ddSRafael J. Wysocki ret_freq = cpufreq_driver->get(cpu); 13821da177e4SLinus Torvalds 1383e08f5f5bSGautham R Shenoy if (ret_freq && policy->cur && 13841c3d85ddSRafael J. Wysocki !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1385e08f5f5bSGautham R Shenoy /* verify no discrepancy between actual and 1386e08f5f5bSGautham R Shenoy saved value exists */ 1387e08f5f5bSGautham R Shenoy if (unlikely(ret_freq != policy->cur)) { 1388e08f5f5bSGautham R Shenoy cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 13891da177e4SLinus Torvalds schedule_work(&policy->update); 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds } 13921da177e4SLinus Torvalds 13934d34a67dSDave Jones return ret_freq; 13945a01f2e8SVenkatesh Pallipadi } 13951da177e4SLinus Torvalds 13965a01f2e8SVenkatesh Pallipadi /** 13975a01f2e8SVenkatesh Pallipadi * cpufreq_get - get the current CPU frequency (in kHz) 13985a01f2e8SVenkatesh Pallipadi * @cpu: CPU number 13995a01f2e8SVenkatesh Pallipadi * 14005a01f2e8SVenkatesh Pallipadi * Get the CPU current (static) CPU frequency 14015a01f2e8SVenkatesh Pallipadi */ 14025a01f2e8SVenkatesh Pallipadi unsigned int cpufreq_get(unsigned int cpu) 14035a01f2e8SVenkatesh Pallipadi { 14045a01f2e8SVenkatesh Pallipadi unsigned int ret_freq = 0; 14055a01f2e8SVenkatesh Pallipadi 14066eed9404SViresh Kumar if (!down_read_trylock(&cpufreq_rwsem)) 14076eed9404SViresh Kumar return 0; 14085a01f2e8SVenkatesh Pallipadi 14095a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_read(cpu))) 14105a01f2e8SVenkatesh Pallipadi goto out_policy; 14115a01f2e8SVenkatesh Pallipadi 14125a01f2e8SVenkatesh Pallipadi ret_freq = __cpufreq_get(cpu); 14135a01f2e8SVenkatesh Pallipadi 14145a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_read(cpu); 14155a01f2e8SVenkatesh Pallipadi 14165a01f2e8SVenkatesh Pallipadi out_policy: 14176eed9404SViresh Kumar up_read(&cpufreq_rwsem); 14186eed9404SViresh Kumar 14194d34a67dSDave Jones return ret_freq; 14201da177e4SLinus Torvalds } 14211da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get); 14221da177e4SLinus Torvalds 14238a25a2fdSKay Sievers static struct subsys_interface cpufreq_interface = { 14248a25a2fdSKay Sievers .name = "cpufreq", 14258a25a2fdSKay Sievers .subsys = &cpu_subsys, 14268a25a2fdSKay Sievers .add_dev = cpufreq_add_dev, 14278a25a2fdSKay Sievers .remove_dev = cpufreq_remove_dev, 1428e00e56dfSRafael J. Wysocki }; 1429e00e56dfSRafael J. Wysocki 14301da177e4SLinus Torvalds /** 1431e00e56dfSRafael J. Wysocki * cpufreq_bp_suspend - Prepare the boot CPU for system suspend. 1432e00e56dfSRafael J. Wysocki * 1433e00e56dfSRafael J. Wysocki * This function is only executed for the boot processor. The other CPUs 1434e00e56dfSRafael J. Wysocki * have been put offline by means of CPU hotplug. 143542d4dc3fSBenjamin Herrenschmidt */ 1436e00e56dfSRafael J. Wysocki static int cpufreq_bp_suspend(void) 143742d4dc3fSBenjamin Herrenschmidt { 1438e08f5f5bSGautham R Shenoy int ret = 0; 14394bc5d341SDave Jones 1440e00e56dfSRafael J. Wysocki int cpu = smp_processor_id(); 14413a3e9e06SViresh Kumar struct cpufreq_policy *policy; 144242d4dc3fSBenjamin Herrenschmidt 14432d06d8c4SDominik Brodowski pr_debug("suspending cpu %u\n", cpu); 144442d4dc3fSBenjamin Herrenschmidt 1445e00e56dfSRafael J. Wysocki /* If there's no policy for the boot CPU, we have nothing to do. */ 14463a3e9e06SViresh Kumar policy = cpufreq_cpu_get(cpu); 14473a3e9e06SViresh Kumar if (!policy) 1448e00e56dfSRafael J. Wysocki return 0; 144942d4dc3fSBenjamin Herrenschmidt 14501c3d85ddSRafael J. Wysocki if (cpufreq_driver->suspend) { 14513a3e9e06SViresh Kumar ret = cpufreq_driver->suspend(policy); 1452ce6c3997SDominik Brodowski if (ret) 145342d4dc3fSBenjamin Herrenschmidt printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 14543a3e9e06SViresh Kumar "step on CPU %u\n", policy->cpu); 145542d4dc3fSBenjamin Herrenschmidt } 145642d4dc3fSBenjamin Herrenschmidt 14573a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 1458c9060494SDave Jones return ret; 145942d4dc3fSBenjamin Herrenschmidt } 146042d4dc3fSBenjamin Herrenschmidt 146142d4dc3fSBenjamin Herrenschmidt /** 1462e00e56dfSRafael J. Wysocki * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU. 14631da177e4SLinus Torvalds * 14641da177e4SLinus Torvalds * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 1465ce6c3997SDominik Brodowski * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are 1466ce6c3997SDominik Brodowski * restored. It will verify that the current freq is in sync with 1467ce6c3997SDominik Brodowski * what we believe it to be. This is a bit later than when it 1468ce6c3997SDominik Brodowski * should be, but nonethteless it's better than calling 1469ce6c3997SDominik Brodowski * cpufreq_driver->get() here which might re-enable interrupts... 1470e00e56dfSRafael J. Wysocki * 1471e00e56dfSRafael J. Wysocki * This function is only executed for the boot CPU. The other CPUs have not 1472e00e56dfSRafael J. Wysocki * been turned on yet. 14731da177e4SLinus Torvalds */ 1474e00e56dfSRafael J. Wysocki static void cpufreq_bp_resume(void) 14751da177e4SLinus Torvalds { 1476e08f5f5bSGautham R Shenoy int ret = 0; 14774bc5d341SDave Jones 1478e00e56dfSRafael J. Wysocki int cpu = smp_processor_id(); 14793a3e9e06SViresh Kumar struct cpufreq_policy *policy; 14801da177e4SLinus Torvalds 14812d06d8c4SDominik Brodowski pr_debug("resuming cpu %u\n", cpu); 14821da177e4SLinus Torvalds 1483e00e56dfSRafael J. Wysocki /* If there's no policy for the boot CPU, we have nothing to do. */ 14843a3e9e06SViresh Kumar policy = cpufreq_cpu_get(cpu); 14853a3e9e06SViresh Kumar if (!policy) 1486e00e56dfSRafael J. Wysocki return; 14871da177e4SLinus Torvalds 14881c3d85ddSRafael J. Wysocki if (cpufreq_driver->resume) { 14893a3e9e06SViresh Kumar ret = cpufreq_driver->resume(policy); 14901da177e4SLinus Torvalds if (ret) { 14911da177e4SLinus Torvalds printk(KERN_ERR "cpufreq: resume failed in ->resume " 14923a3e9e06SViresh Kumar "step on CPU %u\n", policy->cpu); 1493c9060494SDave Jones goto fail; 14941da177e4SLinus Torvalds } 14951da177e4SLinus Torvalds } 14961da177e4SLinus Torvalds 14973a3e9e06SViresh Kumar schedule_work(&policy->update); 1498ce6c3997SDominik Brodowski 1499c9060494SDave Jones fail: 15003a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 15011da177e4SLinus Torvalds } 15021da177e4SLinus Torvalds 1503e00e56dfSRafael J. Wysocki static struct syscore_ops cpufreq_syscore_ops = { 1504e00e56dfSRafael J. Wysocki .suspend = cpufreq_bp_suspend, 1505e00e56dfSRafael J. Wysocki .resume = cpufreq_bp_resume, 15061da177e4SLinus Torvalds }; 15071da177e4SLinus Torvalds 15089d95046eSBorislav Petkov /** 15099d95046eSBorislav Petkov * cpufreq_get_current_driver - return current driver's name 15109d95046eSBorislav Petkov * 15119d95046eSBorislav Petkov * Return the name string of the currently loaded cpufreq driver 15129d95046eSBorislav Petkov * or NULL, if none. 15139d95046eSBorislav Petkov */ 15149d95046eSBorislav Petkov const char *cpufreq_get_current_driver(void) 15159d95046eSBorislav Petkov { 15161c3d85ddSRafael J. Wysocki if (cpufreq_driver) 15171c3d85ddSRafael J. Wysocki return cpufreq_driver->name; 15181c3d85ddSRafael J. Wysocki 15191c3d85ddSRafael J. Wysocki return NULL; 15209d95046eSBorislav Petkov } 15219d95046eSBorislav Petkov EXPORT_SYMBOL_GPL(cpufreq_get_current_driver); 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds /********************************************************************* 15241da177e4SLinus Torvalds * NOTIFIER LISTS INTERFACE * 15251da177e4SLinus Torvalds *********************************************************************/ 15261da177e4SLinus Torvalds 15271da177e4SLinus Torvalds /** 15281da177e4SLinus Torvalds * cpufreq_register_notifier - register a driver with cpufreq 15291da177e4SLinus Torvalds * @nb: notifier function to register 15301da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 15311da177e4SLinus Torvalds * 15321da177e4SLinus Torvalds * Add a driver to one of two lists: either a list of drivers that 15331da177e4SLinus Torvalds * are notified about clock rate changes (once before and once after 15341da177e4SLinus Torvalds * the transition), or a list of drivers that are notified about 15351da177e4SLinus Torvalds * changes in cpufreq policy. 15361da177e4SLinus Torvalds * 15371da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1538e041c683SAlan Stern * blocking_notifier_chain_register. 15391da177e4SLinus Torvalds */ 15401da177e4SLinus Torvalds int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 15411da177e4SLinus Torvalds { 15421da177e4SLinus Torvalds int ret; 15431da177e4SLinus Torvalds 1544d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1545d5aaffa9SDirk Brandewie return -EINVAL; 1546d5aaffa9SDirk Brandewie 154774212ca4SCesar Eduardo Barros WARN_ON(!init_cpufreq_transition_notifier_list_called); 154874212ca4SCesar Eduardo Barros 15491da177e4SLinus Torvalds switch (list) { 15501da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1551b4dfdbb3SAlan Stern ret = srcu_notifier_chain_register( 1552e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 15531da177e4SLinus Torvalds break; 15541da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1555e041c683SAlan Stern ret = blocking_notifier_chain_register( 1556e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 15571da177e4SLinus Torvalds break; 15581da177e4SLinus Torvalds default: 15591da177e4SLinus Torvalds ret = -EINVAL; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 15621da177e4SLinus Torvalds return ret; 15631da177e4SLinus Torvalds } 15641da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_register_notifier); 15651da177e4SLinus Torvalds 15661da177e4SLinus Torvalds /** 15671da177e4SLinus Torvalds * cpufreq_unregister_notifier - unregister a driver with cpufreq 15681da177e4SLinus Torvalds * @nb: notifier block to be unregistered 15691da177e4SLinus Torvalds * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 15701da177e4SLinus Torvalds * 15711da177e4SLinus Torvalds * Remove a driver from the CPU frequency notifier list. 15721da177e4SLinus Torvalds * 15731da177e4SLinus Torvalds * This function may sleep, and has the same return conditions as 1574e041c683SAlan Stern * blocking_notifier_chain_unregister. 15751da177e4SLinus Torvalds */ 15761da177e4SLinus Torvalds int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 15771da177e4SLinus Torvalds { 15781da177e4SLinus Torvalds int ret; 15791da177e4SLinus Torvalds 1580d5aaffa9SDirk Brandewie if (cpufreq_disabled()) 1581d5aaffa9SDirk Brandewie return -EINVAL; 1582d5aaffa9SDirk Brandewie 15831da177e4SLinus Torvalds switch (list) { 15841da177e4SLinus Torvalds case CPUFREQ_TRANSITION_NOTIFIER: 1585b4dfdbb3SAlan Stern ret = srcu_notifier_chain_unregister( 1586e041c683SAlan Stern &cpufreq_transition_notifier_list, nb); 15871da177e4SLinus Torvalds break; 15881da177e4SLinus Torvalds case CPUFREQ_POLICY_NOTIFIER: 1589e041c683SAlan Stern ret = blocking_notifier_chain_unregister( 1590e041c683SAlan Stern &cpufreq_policy_notifier_list, nb); 15911da177e4SLinus Torvalds break; 15921da177e4SLinus Torvalds default: 15931da177e4SLinus Torvalds ret = -EINVAL; 15941da177e4SLinus Torvalds } 15951da177e4SLinus Torvalds 15961da177e4SLinus Torvalds return ret; 15971da177e4SLinus Torvalds } 15981da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_unregister_notifier); 15991da177e4SLinus Torvalds 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds /********************************************************************* 16021da177e4SLinus Torvalds * GOVERNORS * 16031da177e4SLinus Torvalds *********************************************************************/ 16041da177e4SLinus Torvalds 16051da177e4SLinus Torvalds int __cpufreq_driver_target(struct cpufreq_policy *policy, 16061da177e4SLinus Torvalds unsigned int target_freq, 16071da177e4SLinus Torvalds unsigned int relation) 16081da177e4SLinus Torvalds { 16091da177e4SLinus Torvalds int retval = -EINVAL; 16107249924eSViresh Kumar unsigned int old_target_freq = target_freq; 1611c32b6b8eSAshok Raj 1612a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1613a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 16147c30ed53SViresh Kumar if (policy->transition_ongoing) 16157c30ed53SViresh Kumar return -EBUSY; 1616a7b422cdSKonrad Rzeszutek Wilk 16177249924eSViresh Kumar /* Make sure that target_freq is within supported range */ 16187249924eSViresh Kumar if (target_freq > policy->max) 16197249924eSViresh Kumar target_freq = policy->max; 16207249924eSViresh Kumar if (target_freq < policy->min) 16217249924eSViresh Kumar target_freq = policy->min; 16227249924eSViresh Kumar 16237249924eSViresh Kumar pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n", 16247249924eSViresh Kumar policy->cpu, target_freq, relation, old_target_freq); 16255a1c0228SViresh Kumar 16265a1c0228SViresh Kumar if (target_freq == policy->cur) 16275a1c0228SViresh Kumar return 0; 16285a1c0228SViresh Kumar 16291c3d85ddSRafael J. Wysocki if (cpufreq_driver->target) 16301c3d85ddSRafael J. Wysocki retval = cpufreq_driver->target(policy, target_freq, relation); 163190d45d17SAshok Raj 16321da177e4SLinus Torvalds return retval; 16331da177e4SLinus Torvalds } 16341da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 16351da177e4SLinus Torvalds 16361da177e4SLinus Torvalds int cpufreq_driver_target(struct cpufreq_policy *policy, 16371da177e4SLinus Torvalds unsigned int target_freq, 16381da177e4SLinus Torvalds unsigned int relation) 16391da177e4SLinus Torvalds { 1640f1829e4aSJulia Lawall int ret = -EINVAL; 16411da177e4SLinus Torvalds 16425a01f2e8SVenkatesh Pallipadi if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1643f1829e4aSJulia Lawall goto fail; 16441da177e4SLinus Torvalds 16451da177e4SLinus Torvalds ret = __cpufreq_driver_target(policy, target_freq, relation); 16461da177e4SLinus Torvalds 16475a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(policy->cpu); 16481da177e4SLinus Torvalds 1649f1829e4aSJulia Lawall fail: 16501da177e4SLinus Torvalds return ret; 16511da177e4SLinus Torvalds } 16521da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_driver_target); 16531da177e4SLinus Torvalds 1654153d7f3fSArjan van de Ven /* 1655153d7f3fSArjan van de Ven * when "event" is CPUFREQ_GOV_LIMITS 1656153d7f3fSArjan van de Ven */ 16571da177e4SLinus Torvalds 1658e08f5f5bSGautham R Shenoy static int __cpufreq_governor(struct cpufreq_policy *policy, 1659e08f5f5bSGautham R Shenoy unsigned int event) 16601da177e4SLinus Torvalds { 1661cc993cabSDave Jones int ret; 16626afde10cSThomas Renninger 16636afde10cSThomas Renninger /* Only must be defined when default governor is known to have latency 16646afde10cSThomas Renninger restrictions, like e.g. conservative or ondemand. 16656afde10cSThomas Renninger That this is the case is already ensured in Kconfig 16666afde10cSThomas Renninger */ 16676afde10cSThomas Renninger #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 16686afde10cSThomas Renninger struct cpufreq_governor *gov = &cpufreq_gov_performance; 16696afde10cSThomas Renninger #else 16706afde10cSThomas Renninger struct cpufreq_governor *gov = NULL; 16716afde10cSThomas Renninger #endif 16721c256245SThomas Renninger 16731c256245SThomas Renninger if (policy->governor->max_transition_latency && 16741c256245SThomas Renninger policy->cpuinfo.transition_latency > 16751c256245SThomas Renninger policy->governor->max_transition_latency) { 16766afde10cSThomas Renninger if (!gov) 16776afde10cSThomas Renninger return -EINVAL; 16786afde10cSThomas Renninger else { 16791c256245SThomas Renninger printk(KERN_WARNING "%s governor failed, too long" 16801c256245SThomas Renninger " transition latency of HW, fallback" 16811c256245SThomas Renninger " to %s governor\n", 16821c256245SThomas Renninger policy->governor->name, 16831c256245SThomas Renninger gov->name); 16841c256245SThomas Renninger policy->governor = gov; 16851c256245SThomas Renninger } 16866afde10cSThomas Renninger } 16871da177e4SLinus Torvalds 1688fe492f3fSViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 16891da177e4SLinus Torvalds if (!try_module_get(policy->governor->owner)) 16901da177e4SLinus Torvalds return -EINVAL; 16911da177e4SLinus Torvalds 16922d06d8c4SDominik Brodowski pr_debug("__cpufreq_governor for CPU %u, event %u\n", 1693e08f5f5bSGautham R Shenoy policy->cpu, event); 169495731ebbSXiaoguang Chen 169595731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 169695731ebbSXiaoguang Chen if ((!policy->governor_enabled && (event == CPUFREQ_GOV_STOP)) || 169795731ebbSXiaoguang Chen (policy->governor_enabled && (event == CPUFREQ_GOV_START))) { 169895731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 169995731ebbSXiaoguang Chen return -EBUSY; 170095731ebbSXiaoguang Chen } 170195731ebbSXiaoguang Chen 170295731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 170395731ebbSXiaoguang Chen policy->governor_enabled = false; 170495731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 170595731ebbSXiaoguang Chen policy->governor_enabled = true; 170695731ebbSXiaoguang Chen 170795731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 170895731ebbSXiaoguang Chen 17091da177e4SLinus Torvalds ret = policy->governor->governor(policy, event); 17101da177e4SLinus Torvalds 17114d5dcc42SViresh Kumar if (!ret) { 17124d5dcc42SViresh Kumar if (event == CPUFREQ_GOV_POLICY_INIT) 17138e53695fSViresh Kumar policy->governor->initialized++; 17144d5dcc42SViresh Kumar else if (event == CPUFREQ_GOV_POLICY_EXIT) 17158e53695fSViresh Kumar policy->governor->initialized--; 171695731ebbSXiaoguang Chen } else { 171795731ebbSXiaoguang Chen /* Restore original values */ 171895731ebbSXiaoguang Chen mutex_lock(&cpufreq_governor_lock); 171995731ebbSXiaoguang Chen if (event == CPUFREQ_GOV_STOP) 172095731ebbSXiaoguang Chen policy->governor_enabled = true; 172195731ebbSXiaoguang Chen else if (event == CPUFREQ_GOV_START) 172295731ebbSXiaoguang Chen policy->governor_enabled = false; 172395731ebbSXiaoguang Chen mutex_unlock(&cpufreq_governor_lock); 17244d5dcc42SViresh Kumar } 1725b394058fSViresh Kumar 1726fe492f3fSViresh Kumar if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) || 1727fe492f3fSViresh Kumar ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret)) 17281da177e4SLinus Torvalds module_put(policy->governor->owner); 17291da177e4SLinus Torvalds 17301da177e4SLinus Torvalds return ret; 17311da177e4SLinus Torvalds } 17321da177e4SLinus Torvalds 17331da177e4SLinus Torvalds int cpufreq_register_governor(struct cpufreq_governor *governor) 17341da177e4SLinus Torvalds { 17353bcb09a3SJeremy Fitzhardinge int err; 17361da177e4SLinus Torvalds 17371da177e4SLinus Torvalds if (!governor) 17381da177e4SLinus Torvalds return -EINVAL; 17391da177e4SLinus Torvalds 1740a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1741a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 1742a7b422cdSKonrad Rzeszutek Wilk 17433fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 17441da177e4SLinus Torvalds 1745b394058fSViresh Kumar governor->initialized = 0; 17463bcb09a3SJeremy Fitzhardinge err = -EBUSY; 17473bcb09a3SJeremy Fitzhardinge if (__find_governor(governor->name) == NULL) { 17483bcb09a3SJeremy Fitzhardinge err = 0; 17491da177e4SLinus Torvalds list_add(&governor->governor_list, &cpufreq_governor_list); 17503bcb09a3SJeremy Fitzhardinge } 17511da177e4SLinus Torvalds 17523fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 17533bcb09a3SJeremy Fitzhardinge return err; 17541da177e4SLinus Torvalds } 17551da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_governor); 17561da177e4SLinus Torvalds 17571da177e4SLinus Torvalds void cpufreq_unregister_governor(struct cpufreq_governor *governor) 17581da177e4SLinus Torvalds { 175990e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU 176090e41bacSPrarit Bhargava int cpu; 176190e41bacSPrarit Bhargava #endif 176290e41bacSPrarit Bhargava 17631da177e4SLinus Torvalds if (!governor) 17641da177e4SLinus Torvalds return; 17651da177e4SLinus Torvalds 1766a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 1767a7b422cdSKonrad Rzeszutek Wilk return; 1768a7b422cdSKonrad Rzeszutek Wilk 176990e41bacSPrarit Bhargava #ifdef CONFIG_HOTPLUG_CPU 177090e41bacSPrarit Bhargava for_each_present_cpu(cpu) { 177190e41bacSPrarit Bhargava if (cpu_online(cpu)) 177290e41bacSPrarit Bhargava continue; 177390e41bacSPrarit Bhargava if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name)) 177490e41bacSPrarit Bhargava strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0"); 177590e41bacSPrarit Bhargava } 177690e41bacSPrarit Bhargava #endif 177790e41bacSPrarit Bhargava 17783fc54d37Sakpm@osdl.org mutex_lock(&cpufreq_governor_mutex); 17791da177e4SLinus Torvalds list_del(&governor->governor_list); 17803fc54d37Sakpm@osdl.org mutex_unlock(&cpufreq_governor_mutex); 17811da177e4SLinus Torvalds return; 17821da177e4SLinus Torvalds } 17831da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 17841da177e4SLinus Torvalds 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds /********************************************************************* 17871da177e4SLinus Torvalds * POLICY INTERFACE * 17881da177e4SLinus Torvalds *********************************************************************/ 17891da177e4SLinus Torvalds 17901da177e4SLinus Torvalds /** 17911da177e4SLinus Torvalds * cpufreq_get_policy - get the current cpufreq_policy 179229464f28SDave Jones * @policy: struct cpufreq_policy into which the current cpufreq_policy 179329464f28SDave Jones * is written 17941da177e4SLinus Torvalds * 17951da177e4SLinus Torvalds * Reads the current cpufreq policy. 17961da177e4SLinus Torvalds */ 17971da177e4SLinus Torvalds int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 17981da177e4SLinus Torvalds { 17991da177e4SLinus Torvalds struct cpufreq_policy *cpu_policy; 18001da177e4SLinus Torvalds if (!policy) 18011da177e4SLinus Torvalds return -EINVAL; 18021da177e4SLinus Torvalds 18031da177e4SLinus Torvalds cpu_policy = cpufreq_cpu_get(cpu); 18041da177e4SLinus Torvalds if (!cpu_policy) 18051da177e4SLinus Torvalds return -EINVAL; 18061da177e4SLinus Torvalds 1807d5b73cd8SViresh Kumar memcpy(policy, cpu_policy, sizeof(*policy)); 18081da177e4SLinus Torvalds 18091da177e4SLinus Torvalds cpufreq_cpu_put(cpu_policy); 18101da177e4SLinus Torvalds return 0; 18111da177e4SLinus Torvalds } 18121da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_get_policy); 18131da177e4SLinus Torvalds 1814153d7f3fSArjan van de Ven /* 1815e08f5f5bSGautham R Shenoy * data : current policy. 1816e08f5f5bSGautham R Shenoy * policy : policy to be set. 1817153d7f3fSArjan van de Ven */ 18183a3e9e06SViresh Kumar static int __cpufreq_set_policy(struct cpufreq_policy *policy, 18193a3e9e06SViresh Kumar struct cpufreq_policy *new_policy) 18201da177e4SLinus Torvalds { 18217bd353a9SViresh Kumar int ret = 0, failed = 1; 18221da177e4SLinus Torvalds 18233a3e9e06SViresh Kumar pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu, 18243a3e9e06SViresh Kumar new_policy->min, new_policy->max); 18251da177e4SLinus Torvalds 1826d5b73cd8SViresh Kumar memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo)); 18271da177e4SLinus Torvalds 18283a3e9e06SViresh Kumar if (new_policy->min > policy->max || new_policy->max < policy->min) { 18299c9a43edSMattia Dongili ret = -EINVAL; 18309c9a43edSMattia Dongili goto error_out; 18319c9a43edSMattia Dongili } 18329c9a43edSMattia Dongili 18331da177e4SLinus Torvalds /* verify the cpu speed can be set within this limit */ 18343a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 18351da177e4SLinus Torvalds if (ret) 18361da177e4SLinus Torvalds goto error_out; 18371da177e4SLinus Torvalds 18381da177e4SLinus Torvalds /* adjust if necessary - all reasons */ 1839e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 18403a3e9e06SViresh Kumar CPUFREQ_ADJUST, new_policy); 18411da177e4SLinus Torvalds 18421da177e4SLinus Torvalds /* adjust if necessary - hardware incompatibility*/ 1843e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 18443a3e9e06SViresh Kumar CPUFREQ_INCOMPATIBLE, new_policy); 18451da177e4SLinus Torvalds 1846bb176f7dSViresh Kumar /* 1847bb176f7dSViresh Kumar * verify the cpu speed can be set within this limit, which might be 1848bb176f7dSViresh Kumar * different to the first one 1849bb176f7dSViresh Kumar */ 18503a3e9e06SViresh Kumar ret = cpufreq_driver->verify(new_policy); 1851e041c683SAlan Stern if (ret) 18521da177e4SLinus Torvalds goto error_out; 18531da177e4SLinus Torvalds 18541da177e4SLinus Torvalds /* notification of the new policy */ 1855e041c683SAlan Stern blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 18563a3e9e06SViresh Kumar CPUFREQ_NOTIFY, new_policy); 18571da177e4SLinus Torvalds 18583a3e9e06SViresh Kumar policy->min = new_policy->min; 18593a3e9e06SViresh Kumar policy->max = new_policy->max; 18601da177e4SLinus Torvalds 18612d06d8c4SDominik Brodowski pr_debug("new min and max freqs are %u - %u kHz\n", 18623a3e9e06SViresh Kumar policy->min, policy->max); 18631da177e4SLinus Torvalds 18641c3d85ddSRafael J. Wysocki if (cpufreq_driver->setpolicy) { 18653a3e9e06SViresh Kumar policy->policy = new_policy->policy; 18662d06d8c4SDominik Brodowski pr_debug("setting range\n"); 18673a3e9e06SViresh Kumar ret = cpufreq_driver->setpolicy(new_policy); 18681da177e4SLinus Torvalds } else { 18693a3e9e06SViresh Kumar if (new_policy->governor != policy->governor) { 18701da177e4SLinus Torvalds /* save old, working values */ 18713a3e9e06SViresh Kumar struct cpufreq_governor *old_gov = policy->governor; 18721da177e4SLinus Torvalds 18732d06d8c4SDominik Brodowski pr_debug("governor switch\n"); 18741da177e4SLinus Torvalds 18751da177e4SLinus Torvalds /* end old governor */ 18763a3e9e06SViresh Kumar if (policy->governor) { 18773a3e9e06SViresh Kumar __cpufreq_governor(policy, CPUFREQ_GOV_STOP); 18783a3e9e06SViresh Kumar unlock_policy_rwsem_write(new_policy->cpu); 18793a3e9e06SViresh Kumar __cpufreq_governor(policy, 18807bd353a9SViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 18813a3e9e06SViresh Kumar lock_policy_rwsem_write(new_policy->cpu); 18827bd353a9SViresh Kumar } 18831da177e4SLinus Torvalds 18841da177e4SLinus Torvalds /* start new governor */ 18853a3e9e06SViresh Kumar policy->governor = new_policy->governor; 18863a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) { 18873a3e9e06SViresh Kumar if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) { 18887bd353a9SViresh Kumar failed = 0; 1889955ef483SViresh Kumar } else { 18903a3e9e06SViresh Kumar unlock_policy_rwsem_write(new_policy->cpu); 18913a3e9e06SViresh Kumar __cpufreq_governor(policy, 18927bd353a9SViresh Kumar CPUFREQ_GOV_POLICY_EXIT); 18933a3e9e06SViresh Kumar lock_policy_rwsem_write(new_policy->cpu); 1894955ef483SViresh Kumar } 18957bd353a9SViresh Kumar } 18967bd353a9SViresh Kumar 18977bd353a9SViresh Kumar if (failed) { 18981da177e4SLinus Torvalds /* new governor failed, so re-start old one */ 18992d06d8c4SDominik Brodowski pr_debug("starting governor %s failed\n", 19003a3e9e06SViresh Kumar policy->governor->name); 19011da177e4SLinus Torvalds if (old_gov) { 19023a3e9e06SViresh Kumar policy->governor = old_gov; 19033a3e9e06SViresh Kumar __cpufreq_governor(policy, 19047bd353a9SViresh Kumar CPUFREQ_GOV_POLICY_INIT); 19053a3e9e06SViresh Kumar __cpufreq_governor(policy, 1906e08f5f5bSGautham R Shenoy CPUFREQ_GOV_START); 19071da177e4SLinus Torvalds } 19081da177e4SLinus Torvalds ret = -EINVAL; 19091da177e4SLinus Torvalds goto error_out; 19101da177e4SLinus Torvalds } 19111da177e4SLinus Torvalds /* might be a policy change, too, so fall through */ 19121da177e4SLinus Torvalds } 19132d06d8c4SDominik Brodowski pr_debug("governor: change or update limits\n"); 19143de9bdebSViresh Kumar ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS); 19151da177e4SLinus Torvalds } 19161da177e4SLinus Torvalds 19171da177e4SLinus Torvalds error_out: 19181da177e4SLinus Torvalds return ret; 19191da177e4SLinus Torvalds } 19201da177e4SLinus Torvalds 19211da177e4SLinus Torvalds /** 19221da177e4SLinus Torvalds * cpufreq_update_policy - re-evaluate an existing cpufreq policy 19231da177e4SLinus Torvalds * @cpu: CPU which shall be re-evaluated 19241da177e4SLinus Torvalds * 192525985edcSLucas De Marchi * Useful for policy notifiers which have different necessities 19261da177e4SLinus Torvalds * at different times. 19271da177e4SLinus Torvalds */ 19281da177e4SLinus Torvalds int cpufreq_update_policy(unsigned int cpu) 19291da177e4SLinus Torvalds { 19303a3e9e06SViresh Kumar struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 19313a3e9e06SViresh Kumar struct cpufreq_policy new_policy; 1932f1829e4aSJulia Lawall int ret; 19331da177e4SLinus Torvalds 19343a3e9e06SViresh Kumar if (!policy) { 1935f1829e4aSJulia Lawall ret = -ENODEV; 1936f1829e4aSJulia Lawall goto no_policy; 1937f1829e4aSJulia Lawall } 19381da177e4SLinus Torvalds 1939f1829e4aSJulia Lawall if (unlikely(lock_policy_rwsem_write(cpu))) { 1940f1829e4aSJulia Lawall ret = -EINVAL; 1941f1829e4aSJulia Lawall goto fail; 1942f1829e4aSJulia Lawall } 19431da177e4SLinus Torvalds 19442d06d8c4SDominik Brodowski pr_debug("updating policy for CPU %u\n", cpu); 1945d5b73cd8SViresh Kumar memcpy(&new_policy, policy, sizeof(*policy)); 19463a3e9e06SViresh Kumar new_policy.min = policy->user_policy.min; 19473a3e9e06SViresh Kumar new_policy.max = policy->user_policy.max; 19483a3e9e06SViresh Kumar new_policy.policy = policy->user_policy.policy; 19493a3e9e06SViresh Kumar new_policy.governor = policy->user_policy.governor; 19501da177e4SLinus Torvalds 1951bb176f7dSViresh Kumar /* 1952bb176f7dSViresh Kumar * BIOS might change freq behind our back 1953bb176f7dSViresh Kumar * -> ask driver for current freq and notify governors about a change 1954bb176f7dSViresh Kumar */ 19551c3d85ddSRafael J. Wysocki if (cpufreq_driver->get) { 19563a3e9e06SViresh Kumar new_policy.cur = cpufreq_driver->get(cpu); 19573a3e9e06SViresh Kumar if (!policy->cur) { 19582d06d8c4SDominik Brodowski pr_debug("Driver did not initialize current freq"); 19593a3e9e06SViresh Kumar policy->cur = new_policy.cur; 1960a85f7bd3SThomas Renninger } else { 19613a3e9e06SViresh Kumar if (policy->cur != new_policy.cur && cpufreq_driver->target) 19623a3e9e06SViresh Kumar cpufreq_out_of_sync(cpu, policy->cur, 19633a3e9e06SViresh Kumar new_policy.cur); 19640961dd0dSThomas Renninger } 1965a85f7bd3SThomas Renninger } 19660961dd0dSThomas Renninger 19673a3e9e06SViresh Kumar ret = __cpufreq_set_policy(policy, &new_policy); 19681da177e4SLinus Torvalds 19695a01f2e8SVenkatesh Pallipadi unlock_policy_rwsem_write(cpu); 19705a01f2e8SVenkatesh Pallipadi 1971f1829e4aSJulia Lawall fail: 19723a3e9e06SViresh Kumar cpufreq_cpu_put(policy); 1973f1829e4aSJulia Lawall no_policy: 19741da177e4SLinus Torvalds return ret; 19751da177e4SLinus Torvalds } 19761da177e4SLinus Torvalds EXPORT_SYMBOL(cpufreq_update_policy); 19771da177e4SLinus Torvalds 19782760984fSPaul Gortmaker static int cpufreq_cpu_callback(struct notifier_block *nfb, 1979c32b6b8eSAshok Raj unsigned long action, void *hcpu) 1980c32b6b8eSAshok Raj { 1981c32b6b8eSAshok Raj unsigned int cpu = (unsigned long)hcpu; 19828a25a2fdSKay Sievers struct device *dev; 19835302c3fbSSrivatsa S. Bhat bool frozen = false; 1984c32b6b8eSAshok Raj 19858a25a2fdSKay Sievers dev = get_cpu_device(cpu); 19868a25a2fdSKay Sievers if (dev) { 19875302c3fbSSrivatsa S. Bhat 19885302c3fbSSrivatsa S. Bhat if (action & CPU_TASKS_FROZEN) 19895302c3fbSSrivatsa S. Bhat frozen = true; 19905302c3fbSSrivatsa S. Bhat 19915302c3fbSSrivatsa S. Bhat switch (action & ~CPU_TASKS_FROZEN) { 1992c32b6b8eSAshok Raj case CPU_ONLINE: 19935302c3fbSSrivatsa S. Bhat __cpufreq_add_dev(dev, NULL, frozen); 199423d32899SSrivatsa S. Bhat cpufreq_update_policy(cpu); 1995c32b6b8eSAshok Raj break; 19965302c3fbSSrivatsa S. Bhat 1997c32b6b8eSAshok Raj case CPU_DOWN_PREPARE: 19985302c3fbSSrivatsa S. Bhat __cpufreq_remove_dev(dev, NULL, frozen); 1999c32b6b8eSAshok Raj break; 20005302c3fbSSrivatsa S. Bhat 20015a01f2e8SVenkatesh Pallipadi case CPU_DOWN_FAILED: 20025302c3fbSSrivatsa S. Bhat __cpufreq_add_dev(dev, NULL, frozen); 2003c32b6b8eSAshok Raj break; 2004c32b6b8eSAshok Raj } 2005c32b6b8eSAshok Raj } 2006c32b6b8eSAshok Raj return NOTIFY_OK; 2007c32b6b8eSAshok Raj } 2008c32b6b8eSAshok Raj 20099c36f746SNeal Buckendahl static struct notifier_block __refdata cpufreq_cpu_notifier = { 2010c32b6b8eSAshok Raj .notifier_call = cpufreq_cpu_callback, 2011c32b6b8eSAshok Raj }; 20121da177e4SLinus Torvalds 20131da177e4SLinus Torvalds /********************************************************************* 20141da177e4SLinus Torvalds * REGISTER / UNREGISTER CPUFREQ DRIVER * 20151da177e4SLinus Torvalds *********************************************************************/ 20161da177e4SLinus Torvalds 20171da177e4SLinus Torvalds /** 20181da177e4SLinus Torvalds * cpufreq_register_driver - register a CPU Frequency driver 20191da177e4SLinus Torvalds * @driver_data: A struct cpufreq_driver containing the values# 20201da177e4SLinus Torvalds * submitted by the CPU Frequency driver. 20211da177e4SLinus Torvalds * 20221da177e4SLinus Torvalds * Registers a CPU Frequency driver to this core code. This code 20231da177e4SLinus Torvalds * returns zero on success, -EBUSY when another driver got here first 20241da177e4SLinus Torvalds * (and isn't unregistered in the meantime). 20251da177e4SLinus Torvalds * 20261da177e4SLinus Torvalds */ 2027221dee28SLinus Torvalds int cpufreq_register_driver(struct cpufreq_driver *driver_data) 20281da177e4SLinus Torvalds { 20291da177e4SLinus Torvalds unsigned long flags; 20301da177e4SLinus Torvalds int ret; 20311da177e4SLinus Torvalds 2032a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2033a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2034a7b422cdSKonrad Rzeszutek Wilk 20351da177e4SLinus Torvalds if (!driver_data || !driver_data->verify || !driver_data->init || 20361da177e4SLinus Torvalds ((!driver_data->setpolicy) && (!driver_data->target))) 20371da177e4SLinus Torvalds return -EINVAL; 20381da177e4SLinus Torvalds 20392d06d8c4SDominik Brodowski pr_debug("trying to register driver %s\n", driver_data->name); 20401da177e4SLinus Torvalds 20411da177e4SLinus Torvalds if (driver_data->setpolicy) 20421da177e4SLinus Torvalds driver_data->flags |= CPUFREQ_CONST_LOOPS; 20431da177e4SLinus Torvalds 20440d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 20451c3d85ddSRafael J. Wysocki if (cpufreq_driver) { 20460d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 20471da177e4SLinus Torvalds return -EBUSY; 20481da177e4SLinus Torvalds } 20491c3d85ddSRafael J. Wysocki cpufreq_driver = driver_data; 20500d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 20511da177e4SLinus Torvalds 20528a25a2fdSKay Sievers ret = subsys_interface_register(&cpufreq_interface); 20538f5bc2abSJiri Slaby if (ret) 20548f5bc2abSJiri Slaby goto err_null_driver; 20551da177e4SLinus Torvalds 20561c3d85ddSRafael J. Wysocki if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) { 20571da177e4SLinus Torvalds int i; 20581da177e4SLinus Torvalds ret = -ENODEV; 20591da177e4SLinus Torvalds 20601da177e4SLinus Torvalds /* check for at least one working CPU */ 20617a6aedfaSMike Travis for (i = 0; i < nr_cpu_ids; i++) 20627a6aedfaSMike Travis if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 20631da177e4SLinus Torvalds ret = 0; 20647a6aedfaSMike Travis break; 20657a6aedfaSMike Travis } 20661da177e4SLinus Torvalds 20671da177e4SLinus Torvalds /* if all ->init() calls failed, unregister */ 20681da177e4SLinus Torvalds if (ret) { 20692d06d8c4SDominik Brodowski pr_debug("no CPU initialized for driver %s\n", 2070e08f5f5bSGautham R Shenoy driver_data->name); 20718a25a2fdSKay Sievers goto err_if_unreg; 20721da177e4SLinus Torvalds } 20731da177e4SLinus Torvalds } 20741da177e4SLinus Torvalds 207565edc68cSChandra Seetharaman register_hotcpu_notifier(&cpufreq_cpu_notifier); 20762d06d8c4SDominik Brodowski pr_debug("driver %s up and running\n", driver_data->name); 20771da177e4SLinus Torvalds 20788f5bc2abSJiri Slaby return 0; 20798a25a2fdSKay Sievers err_if_unreg: 20808a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 20818f5bc2abSJiri Slaby err_null_driver: 20820d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 20831c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 20840d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 20854d34a67dSDave Jones return ret; 20861da177e4SLinus Torvalds } 20871da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_register_driver); 20881da177e4SLinus Torvalds 20891da177e4SLinus Torvalds /** 20901da177e4SLinus Torvalds * cpufreq_unregister_driver - unregister the current CPUFreq driver 20911da177e4SLinus Torvalds * 20921da177e4SLinus Torvalds * Unregister the current CPUFreq driver. Only call this if you have 20931da177e4SLinus Torvalds * the right to do so, i.e. if you have succeeded in initialising before! 20941da177e4SLinus Torvalds * Returns zero if successful, and -EINVAL if the cpufreq_driver is 20951da177e4SLinus Torvalds * currently not initialised. 20961da177e4SLinus Torvalds */ 2097221dee28SLinus Torvalds int cpufreq_unregister_driver(struct cpufreq_driver *driver) 20981da177e4SLinus Torvalds { 20991da177e4SLinus Torvalds unsigned long flags; 21001da177e4SLinus Torvalds 21011c3d85ddSRafael J. Wysocki if (!cpufreq_driver || (driver != cpufreq_driver)) 21021da177e4SLinus Torvalds return -EINVAL; 21031da177e4SLinus Torvalds 21042d06d8c4SDominik Brodowski pr_debug("unregistering driver %s\n", driver->name); 21051da177e4SLinus Torvalds 21068a25a2fdSKay Sievers subsys_interface_unregister(&cpufreq_interface); 210765edc68cSChandra Seetharaman unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 21081da177e4SLinus Torvalds 21096eed9404SViresh Kumar down_write(&cpufreq_rwsem); 21100d1857a1SNathan Zimmer write_lock_irqsave(&cpufreq_driver_lock, flags); 21116eed9404SViresh Kumar 21121c3d85ddSRafael J. Wysocki cpufreq_driver = NULL; 21136eed9404SViresh Kumar 21140d1857a1SNathan Zimmer write_unlock_irqrestore(&cpufreq_driver_lock, flags); 21156eed9404SViresh Kumar up_write(&cpufreq_rwsem); 21161da177e4SLinus Torvalds 21171da177e4SLinus Torvalds return 0; 21181da177e4SLinus Torvalds } 21191da177e4SLinus Torvalds EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 21205a01f2e8SVenkatesh Pallipadi 21215a01f2e8SVenkatesh Pallipadi static int __init cpufreq_core_init(void) 21225a01f2e8SVenkatesh Pallipadi { 21235a01f2e8SVenkatesh Pallipadi int cpu; 21245a01f2e8SVenkatesh Pallipadi 2125a7b422cdSKonrad Rzeszutek Wilk if (cpufreq_disabled()) 2126a7b422cdSKonrad Rzeszutek Wilk return -ENODEV; 2127a7b422cdSKonrad Rzeszutek Wilk 2128*474deff7SViresh Kumar for_each_possible_cpu(cpu) 21295a01f2e8SVenkatesh Pallipadi init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 21308aa84ad8SThomas Renninger 21312361be23SViresh Kumar cpufreq_global_kobject = kobject_create(); 21328aa84ad8SThomas Renninger BUG_ON(!cpufreq_global_kobject); 2133e00e56dfSRafael J. Wysocki register_syscore_ops(&cpufreq_syscore_ops); 21348aa84ad8SThomas Renninger 21355a01f2e8SVenkatesh Pallipadi return 0; 21365a01f2e8SVenkatesh Pallipadi } 21375a01f2e8SVenkatesh Pallipadi core_initcall(cpufreq_core_init); 2138