1/* 2 * linux/drivers/cpufreq/cpufreq.c 3 * 4 * Copyright (C) 2001 Russell King 5 * (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de> 6 * 7 * Oct 2005 - Ashok Raj <ashok.raj@intel.com> 8 * Added handling for CPU hotplug 9 * Feb 2006 - Jacob Shin <jacob.shin@amd.com> 10 * Fix handling for CPU hotplug -- affected CPUs 11 * 12 * This program is free software; you can redistribute it and/or modify 13 * it under the terms of the GNU General Public License version 2 as 14 * published by the Free Software Foundation. 15 * 16 */ 17 18#include <linux/kernel.h> 19#include <linux/module.h> 20#include <linux/init.h> 21#include <linux/notifier.h> 22#include <linux/cpufreq.h> 23#include <linux/delay.h> 24#include <linux/interrupt.h> 25#include <linux/spinlock.h> 26#include <linux/device.h> 27#include <linux/slab.h> 28#include <linux/cpu.h> 29#include <linux/completion.h> 30#include <linux/mutex.h> 31 32#include <trace/events/power.h> 33 34#define dprintk(msg...) cpufreq_debug_printk(CPUFREQ_DEBUG_CORE, \ 35 "cpufreq-core", msg) 36 37/** 38 * The "cpufreq driver" - the arch- or hardware-dependent low 39 * level driver of CPUFreq support, and its spinlock. This lock 40 * also protects the cpufreq_cpu_data array. 41 */ 42static struct cpufreq_driver *cpufreq_driver; 43static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 44#ifdef CONFIG_HOTPLUG_CPU 45/* This one keeps track of the previously set governor of a removed CPU */ 46static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor); 47#endif 48static DEFINE_SPINLOCK(cpufreq_driver_lock); 49 50/* 51 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure 52 * all cpufreq/hotplug/workqueue/etc related lock issues. 53 * 54 * The rules for this semaphore: 55 * - Any routine that wants to read from the policy structure will 56 * do a down_read on this semaphore. 57 * - Any routine that will write to the policy structure and/or may take away 58 * the policy altogether (eg. CPU hotplug), will hold this lock in write 59 * mode before doing so. 60 * 61 * Additional rules: 62 * - All holders of the lock should check to make sure that the CPU they 63 * are concerned with are online after they get the lock. 64 * - Governor routines that can be called in cpufreq hotplug path should not 65 * take this sem as top level hotplug notifier handler takes this. 66 * - Lock should not be held across 67 * __cpufreq_governor(data, CPUFREQ_GOV_STOP); 68 */ 69static DEFINE_PER_CPU(int, cpufreq_policy_cpu); 70static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem); 71 72#define lock_policy_rwsem(mode, cpu) \ 73static int lock_policy_rwsem_##mode \ 74(int cpu) \ 75{ \ 76 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \ 77 BUG_ON(policy_cpu == -1); \ 78 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 79 if (unlikely(!cpu_online(cpu))) { \ 80 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \ 81 return -1; \ 82 } \ 83 \ 84 return 0; \ 85} 86 87lock_policy_rwsem(read, cpu); 88 89lock_policy_rwsem(write, cpu); 90 91static void unlock_policy_rwsem_read(int cpu) 92{ 93 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); 94 BUG_ON(policy_cpu == -1); 95 up_read(&per_cpu(cpu_policy_rwsem, policy_cpu)); 96} 97 98static void unlock_policy_rwsem_write(int cpu) 99{ 100 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); 101 BUG_ON(policy_cpu == -1); 102 up_write(&per_cpu(cpu_policy_rwsem, policy_cpu)); 103} 104 105 106/* internal prototypes */ 107static int __cpufreq_governor(struct cpufreq_policy *policy, 108 unsigned int event); 109static unsigned int __cpufreq_get(unsigned int cpu); 110static void handle_update(struct work_struct *work); 111 112/** 113 * Two notifier lists: the "policy" list is involved in the 114 * validation process for a new CPU frequency policy; the 115 * "transition" list for kernel code that needs to handle 116 * changes to devices when the CPU clock speed changes. 117 * The mutex locks both lists. 118 */ 119static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list); 120static struct srcu_notifier_head cpufreq_transition_notifier_list; 121 122static bool init_cpufreq_transition_notifier_list_called; 123static int __init init_cpufreq_transition_notifier_list(void) 124{ 125 srcu_init_notifier_head(&cpufreq_transition_notifier_list); 126 init_cpufreq_transition_notifier_list_called = true; 127 return 0; 128} 129pure_initcall(init_cpufreq_transition_notifier_list); 130 131static LIST_HEAD(cpufreq_governor_list); 132static DEFINE_MUTEX(cpufreq_governor_mutex); 133 134struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 135{ 136 struct cpufreq_policy *data; 137 unsigned long flags; 138 139 if (cpu >= nr_cpu_ids) 140 goto err_out; 141 142 /* get the cpufreq driver */ 143 spin_lock_irqsave(&cpufreq_driver_lock, flags); 144 145 if (!cpufreq_driver) 146 goto err_out_unlock; 147 148 if (!try_module_get(cpufreq_driver->owner)) 149 goto err_out_unlock; 150 151 152 /* get the CPU */ 153 data = per_cpu(cpufreq_cpu_data, cpu); 154 155 if (!data) 156 goto err_out_put_module; 157 158 if (!kobject_get(&data->kobj)) 159 goto err_out_put_module; 160 161 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 162 return data; 163 164err_out_put_module: 165 module_put(cpufreq_driver->owner); 166err_out_unlock: 167 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 168err_out: 169 return NULL; 170} 171EXPORT_SYMBOL_GPL(cpufreq_cpu_get); 172 173 174void cpufreq_cpu_put(struct cpufreq_policy *data) 175{ 176 kobject_put(&data->kobj); 177 module_put(cpufreq_driver->owner); 178} 179EXPORT_SYMBOL_GPL(cpufreq_cpu_put); 180 181 182/********************************************************************* 183 * UNIFIED DEBUG HELPERS * 184 *********************************************************************/ 185#ifdef CONFIG_CPU_FREQ_DEBUG 186 187/* what part(s) of the CPUfreq subsystem are debugged? */ 188static unsigned int debug; 189 190/* is the debug output ratelimit'ed using printk_ratelimit? User can 191 * set or modify this value. 192 */ 193static unsigned int debug_ratelimit = 1; 194 195/* is the printk_ratelimit'ing enabled? It's enabled after a successful 196 * loading of a cpufreq driver, temporarily disabled when a new policy 197 * is set, and disabled upon cpufreq driver removal 198 */ 199static unsigned int disable_ratelimit = 1; 200static DEFINE_SPINLOCK(disable_ratelimit_lock); 201 202static void cpufreq_debug_enable_ratelimit(void) 203{ 204 unsigned long flags; 205 206 spin_lock_irqsave(&disable_ratelimit_lock, flags); 207 if (disable_ratelimit) 208 disable_ratelimit--; 209 spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 210} 211 212static void cpufreq_debug_disable_ratelimit(void) 213{ 214 unsigned long flags; 215 216 spin_lock_irqsave(&disable_ratelimit_lock, flags); 217 disable_ratelimit++; 218 spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 219} 220 221void cpufreq_debug_printk(unsigned int type, const char *prefix, 222 const char *fmt, ...) 223{ 224 char s[256]; 225 va_list args; 226 unsigned int len; 227 unsigned long flags; 228 229 WARN_ON(!prefix); 230 if (type & debug) { 231 spin_lock_irqsave(&disable_ratelimit_lock, flags); 232 if (!disable_ratelimit && debug_ratelimit 233 && !printk_ratelimit()) { 234 spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 235 return; 236 } 237 spin_unlock_irqrestore(&disable_ratelimit_lock, flags); 238 239 len = snprintf(s, 256, KERN_DEBUG "%s: ", prefix); 240 241 va_start(args, fmt); 242 len += vsnprintf(&s[len], (256 - len), fmt, args); 243 va_end(args); 244 245 printk(s); 246 247 WARN_ON(len < 5); 248 } 249} 250EXPORT_SYMBOL(cpufreq_debug_printk); 251 252 253module_param(debug, uint, 0644); 254MODULE_PARM_DESC(debug, "CPUfreq debugging: add 1 to debug core," 255 " 2 to debug drivers, and 4 to debug governors."); 256 257module_param(debug_ratelimit, uint, 0644); 258MODULE_PARM_DESC(debug_ratelimit, "CPUfreq debugging:" 259 " set to 0 to disable ratelimiting."); 260 261#else /* !CONFIG_CPU_FREQ_DEBUG */ 262 263static inline void cpufreq_debug_enable_ratelimit(void) { return; } 264static inline void cpufreq_debug_disable_ratelimit(void) { return; } 265 266#endif /* CONFIG_CPU_FREQ_DEBUG */ 267 268 269/********************************************************************* 270 * EXTERNALLY AFFECTING FREQUENCY CHANGES * 271 *********************************************************************/ 272 273/** 274 * adjust_jiffies - adjust the system "loops_per_jiffy" 275 * 276 * This function alters the system "loops_per_jiffy" for the clock 277 * speed change. Note that loops_per_jiffy cannot be updated on SMP 278 * systems as each CPU might be scaled differently. So, use the arch 279 * per-CPU loops_per_jiffy value wherever possible. 280 */ 281#ifndef CONFIG_SMP 282static unsigned long l_p_j_ref; 283static unsigned int l_p_j_ref_freq; 284 285static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 286{ 287 if (ci->flags & CPUFREQ_CONST_LOOPS) 288 return; 289 290 if (!l_p_j_ref_freq) { 291 l_p_j_ref = loops_per_jiffy; 292 l_p_j_ref_freq = ci->old; 293 dprintk("saving %lu as reference value for loops_per_jiffy; " 294 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq); 295 } 296 if ((val == CPUFREQ_PRECHANGE && ci->old < ci->new) || 297 (val == CPUFREQ_POSTCHANGE && ci->old > ci->new) || 298 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) { 299 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq, 300 ci->new); 301 dprintk("scaling loops_per_jiffy to %lu " 302 "for frequency %u kHz\n", loops_per_jiffy, ci->new); 303 } 304} 305#else 306static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci) 307{ 308 return; 309} 310#endif 311 312 313/** 314 * cpufreq_notify_transition - call notifier chain and adjust_jiffies 315 * on frequency transition. 316 * 317 * This function calls the transition notifiers and the "adjust_jiffies" 318 * function. It is called twice on all CPU frequency changes that have 319 * external effects. 320 */ 321void cpufreq_notify_transition(struct cpufreq_freqs *freqs, unsigned int state) 322{ 323 struct cpufreq_policy *policy; 324 325 BUG_ON(irqs_disabled()); 326 327 freqs->flags = cpufreq_driver->flags; 328 dprintk("notification %u of frequency transition to %u kHz\n", 329 state, freqs->new); 330 331 policy = per_cpu(cpufreq_cpu_data, freqs->cpu); 332 switch (state) { 333 334 case CPUFREQ_PRECHANGE: 335 /* detect if the driver reported a value as "old frequency" 336 * which is not equal to what the cpufreq core thinks is 337 * "old frequency". 338 */ 339 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 340 if ((policy) && (policy->cpu == freqs->cpu) && 341 (policy->cur) && (policy->cur != freqs->old)) { 342 dprintk("Warning: CPU frequency is" 343 " %u, cpufreq assumed %u kHz.\n", 344 freqs->old, policy->cur); 345 freqs->old = policy->cur; 346 } 347 } 348 srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 349 CPUFREQ_PRECHANGE, freqs); 350 adjust_jiffies(CPUFREQ_PRECHANGE, freqs); 351 break; 352 353 case CPUFREQ_POSTCHANGE: 354 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs); 355 dprintk("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new, 356 (unsigned long)freqs->cpu); 357 trace_power_frequency(POWER_PSTATE, freqs->new, freqs->cpu); 358 srcu_notifier_call_chain(&cpufreq_transition_notifier_list, 359 CPUFREQ_POSTCHANGE, freqs); 360 if (likely(policy) && likely(policy->cpu == freqs->cpu)) 361 policy->cur = freqs->new; 362 break; 363 } 364} 365EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 366 367 368 369/********************************************************************* 370 * SYSFS INTERFACE * 371 *********************************************************************/ 372 373static struct cpufreq_governor *__find_governor(const char *str_governor) 374{ 375 struct cpufreq_governor *t; 376 377 list_for_each_entry(t, &cpufreq_governor_list, governor_list) 378 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN)) 379 return t; 380 381 return NULL; 382} 383 384/** 385 * cpufreq_parse_governor - parse a governor string 386 */ 387static int cpufreq_parse_governor(char *str_governor, unsigned int *policy, 388 struct cpufreq_governor **governor) 389{ 390 int err = -EINVAL; 391 392 if (!cpufreq_driver) 393 goto out; 394 395 if (cpufreq_driver->setpolicy) { 396 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) { 397 *policy = CPUFREQ_POLICY_PERFORMANCE; 398 err = 0; 399 } else if (!strnicmp(str_governor, "powersave", 400 CPUFREQ_NAME_LEN)) { 401 *policy = CPUFREQ_POLICY_POWERSAVE; 402 err = 0; 403 } 404 } else if (cpufreq_driver->target) { 405 struct cpufreq_governor *t; 406 407 mutex_lock(&cpufreq_governor_mutex); 408 409 t = __find_governor(str_governor); 410 411 if (t == NULL) { 412 char *name = kasprintf(GFP_KERNEL, "cpufreq_%s", 413 str_governor); 414 415 if (name) { 416 int ret; 417 418 mutex_unlock(&cpufreq_governor_mutex); 419 ret = request_module("%s", name); 420 mutex_lock(&cpufreq_governor_mutex); 421 422 if (ret == 0) 423 t = __find_governor(str_governor); 424 } 425 426 kfree(name); 427 } 428 429 if (t != NULL) { 430 *governor = t; 431 err = 0; 432 } 433 434 mutex_unlock(&cpufreq_governor_mutex); 435 } 436out: 437 return err; 438} 439 440 441/** 442 * cpufreq_per_cpu_attr_read() / show_##file_name() - 443 * print out cpufreq information 444 * 445 * Write out information from cpufreq_driver->policy[cpu]; object must be 446 * "unsigned int". 447 */ 448 449#define show_one(file_name, object) \ 450static ssize_t show_##file_name \ 451(struct cpufreq_policy *policy, char *buf) \ 452{ \ 453 return sprintf(buf, "%u\n", policy->object); \ 454} 455 456show_one(cpuinfo_min_freq, cpuinfo.min_freq); 457show_one(cpuinfo_max_freq, cpuinfo.max_freq); 458show_one(cpuinfo_transition_latency, cpuinfo.transition_latency); 459show_one(scaling_min_freq, min); 460show_one(scaling_max_freq, max); 461show_one(scaling_cur_freq, cur); 462 463static int __cpufreq_set_policy(struct cpufreq_policy *data, 464 struct cpufreq_policy *policy); 465 466/** 467 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access 468 */ 469#define store_one(file_name, object) \ 470static ssize_t store_##file_name \ 471(struct cpufreq_policy *policy, const char *buf, size_t count) \ 472{ \ 473 unsigned int ret = -EINVAL; \ 474 struct cpufreq_policy new_policy; \ 475 \ 476 ret = cpufreq_get_policy(&new_policy, policy->cpu); \ 477 if (ret) \ 478 return -EINVAL; \ 479 \ 480 ret = sscanf(buf, "%u", &new_policy.object); \ 481 if (ret != 1) \ 482 return -EINVAL; \ 483 \ 484 ret = __cpufreq_set_policy(policy, &new_policy); \ 485 policy->user_policy.object = policy->object; \ 486 \ 487 return ret ? ret : count; \ 488} 489 490store_one(scaling_min_freq, min); 491store_one(scaling_max_freq, max); 492 493/** 494 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware 495 */ 496static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy, 497 char *buf) 498{ 499 unsigned int cur_freq = __cpufreq_get(policy->cpu); 500 if (!cur_freq) 501 return sprintf(buf, "<unknown>"); 502 return sprintf(buf, "%u\n", cur_freq); 503} 504 505 506/** 507 * show_scaling_governor - show the current policy for the specified CPU 508 */ 509static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf) 510{ 511 if (policy->policy == CPUFREQ_POLICY_POWERSAVE) 512 return sprintf(buf, "powersave\n"); 513 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE) 514 return sprintf(buf, "performance\n"); 515 else if (policy->governor) 516 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", 517 policy->governor->name); 518 return -EINVAL; 519} 520 521 522/** 523 * store_scaling_governor - store policy for the specified CPU 524 */ 525static ssize_t store_scaling_governor(struct cpufreq_policy *policy, 526 const char *buf, size_t count) 527{ 528 unsigned int ret = -EINVAL; 529 char str_governor[16]; 530 struct cpufreq_policy new_policy; 531 532 ret = cpufreq_get_policy(&new_policy, policy->cpu); 533 if (ret) 534 return ret; 535 536 ret = sscanf(buf, "%15s", str_governor); 537 if (ret != 1) 538 return -EINVAL; 539 540 if (cpufreq_parse_governor(str_governor, &new_policy.policy, 541 &new_policy.governor)) 542 return -EINVAL; 543 544 /* Do not use cpufreq_set_policy here or the user_policy.max 545 will be wrongly overridden */ 546 ret = __cpufreq_set_policy(policy, &new_policy); 547 548 policy->user_policy.policy = policy->policy; 549 policy->user_policy.governor = policy->governor; 550 551 if (ret) 552 return ret; 553 else 554 return count; 555} 556 557/** 558 * show_scaling_driver - show the cpufreq driver currently loaded 559 */ 560static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf) 561{ 562 return scnprintf(buf, CPUFREQ_NAME_LEN, "%s\n", cpufreq_driver->name); 563} 564 565/** 566 * show_scaling_available_governors - show the available CPUfreq governors 567 */ 568static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy, 569 char *buf) 570{ 571 ssize_t i = 0; 572 struct cpufreq_governor *t; 573 574 if (!cpufreq_driver->target) { 575 i += sprintf(buf, "performance powersave"); 576 goto out; 577 } 578 579 list_for_each_entry(t, &cpufreq_governor_list, governor_list) { 580 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char)) 581 - (CPUFREQ_NAME_LEN + 2))) 582 goto out; 583 i += scnprintf(&buf[i], CPUFREQ_NAME_LEN, "%s ", t->name); 584 } 585out: 586 i += sprintf(&buf[i], "\n"); 587 return i; 588} 589 590static ssize_t show_cpus(const struct cpumask *mask, char *buf) 591{ 592 ssize_t i = 0; 593 unsigned int cpu; 594 595 for_each_cpu(cpu, mask) { 596 if (i) 597 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " "); 598 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu); 599 if (i >= (PAGE_SIZE - 5)) 600 break; 601 } 602 i += sprintf(&buf[i], "\n"); 603 return i; 604} 605 606/** 607 * show_related_cpus - show the CPUs affected by each transition even if 608 * hw coordination is in use 609 */ 610static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf) 611{ 612 if (cpumask_empty(policy->related_cpus)) 613 return show_cpus(policy->cpus, buf); 614 return show_cpus(policy->related_cpus, buf); 615} 616 617/** 618 * show_affected_cpus - show the CPUs affected by each transition 619 */ 620static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf) 621{ 622 return show_cpus(policy->cpus, buf); 623} 624 625static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy, 626 const char *buf, size_t count) 627{ 628 unsigned int freq = 0; 629 unsigned int ret; 630 631 if (!policy->governor || !policy->governor->store_setspeed) 632 return -EINVAL; 633 634 ret = sscanf(buf, "%u", &freq); 635 if (ret != 1) 636 return -EINVAL; 637 638 policy->governor->store_setspeed(policy, freq); 639 640 return count; 641} 642 643static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf) 644{ 645 if (!policy->governor || !policy->governor->show_setspeed) 646 return sprintf(buf, "<unsupported>\n"); 647 648 return policy->governor->show_setspeed(policy, buf); 649} 650 651/** 652 * show_scaling_driver - show the current cpufreq HW/BIOS limitation 653 */ 654static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf) 655{ 656 unsigned int limit; 657 int ret; 658 if (cpufreq_driver->bios_limit) { 659 ret = cpufreq_driver->bios_limit(policy->cpu, &limit); 660 if (!ret) 661 return sprintf(buf, "%u\n", limit); 662 } 663 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq); 664} 665 666cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400); 667cpufreq_freq_attr_ro(cpuinfo_min_freq); 668cpufreq_freq_attr_ro(cpuinfo_max_freq); 669cpufreq_freq_attr_ro(cpuinfo_transition_latency); 670cpufreq_freq_attr_ro(scaling_available_governors); 671cpufreq_freq_attr_ro(scaling_driver); 672cpufreq_freq_attr_ro(scaling_cur_freq); 673cpufreq_freq_attr_ro(bios_limit); 674cpufreq_freq_attr_ro(related_cpus); 675cpufreq_freq_attr_ro(affected_cpus); 676cpufreq_freq_attr_rw(scaling_min_freq); 677cpufreq_freq_attr_rw(scaling_max_freq); 678cpufreq_freq_attr_rw(scaling_governor); 679cpufreq_freq_attr_rw(scaling_setspeed); 680 681static struct attribute *default_attrs[] = { 682 &cpuinfo_min_freq.attr, 683 &cpuinfo_max_freq.attr, 684 &cpuinfo_transition_latency.attr, 685 &scaling_min_freq.attr, 686 &scaling_max_freq.attr, 687 &affected_cpus.attr, 688 &related_cpus.attr, 689 &scaling_governor.attr, 690 &scaling_driver.attr, 691 &scaling_available_governors.attr, 692 &scaling_setspeed.attr, 693 NULL 694}; 695 696struct kobject *cpufreq_global_kobject; 697EXPORT_SYMBOL(cpufreq_global_kobject); 698 699#define to_policy(k) container_of(k, struct cpufreq_policy, kobj) 700#define to_attr(a) container_of(a, struct freq_attr, attr) 701 702static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf) 703{ 704 struct cpufreq_policy *policy = to_policy(kobj); 705 struct freq_attr *fattr = to_attr(attr); 706 ssize_t ret = -EINVAL; 707 policy = cpufreq_cpu_get(policy->cpu); 708 if (!policy) 709 goto no_policy; 710 711 if (lock_policy_rwsem_read(policy->cpu) < 0) 712 goto fail; 713 714 if (fattr->show) 715 ret = fattr->show(policy, buf); 716 else 717 ret = -EIO; 718 719 unlock_policy_rwsem_read(policy->cpu); 720fail: 721 cpufreq_cpu_put(policy); 722no_policy: 723 return ret; 724} 725 726static ssize_t store(struct kobject *kobj, struct attribute *attr, 727 const char *buf, size_t count) 728{ 729 struct cpufreq_policy *policy = to_policy(kobj); 730 struct freq_attr *fattr = to_attr(attr); 731 ssize_t ret = -EINVAL; 732 policy = cpufreq_cpu_get(policy->cpu); 733 if (!policy) 734 goto no_policy; 735 736 if (lock_policy_rwsem_write(policy->cpu) < 0) 737 goto fail; 738 739 if (fattr->store) 740 ret = fattr->store(policy, buf, count); 741 else 742 ret = -EIO; 743 744 unlock_policy_rwsem_write(policy->cpu); 745fail: 746 cpufreq_cpu_put(policy); 747no_policy: 748 return ret; 749} 750 751static void cpufreq_sysfs_release(struct kobject *kobj) 752{ 753 struct cpufreq_policy *policy = to_policy(kobj); 754 dprintk("last reference is dropped\n"); 755 complete(&policy->kobj_unregister); 756} 757 758static const struct sysfs_ops sysfs_ops = { 759 .show = show, 760 .store = store, 761}; 762 763static struct kobj_type ktype_cpufreq = { 764 .sysfs_ops = &sysfs_ops, 765 .default_attrs = default_attrs, 766 .release = cpufreq_sysfs_release, 767}; 768 769/* 770 * Returns: 771 * Negative: Failure 772 * 0: Success 773 * Positive: When we have a managed CPU and the sysfs got symlinked 774 */ 775static int cpufreq_add_dev_policy(unsigned int cpu, 776 struct cpufreq_policy *policy, 777 struct sys_device *sys_dev) 778{ 779 int ret = 0; 780#ifdef CONFIG_SMP 781 unsigned long flags; 782 unsigned int j; 783#ifdef CONFIG_HOTPLUG_CPU 784 struct cpufreq_governor *gov; 785 786 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu)); 787 if (gov) { 788 policy->governor = gov; 789 dprintk("Restoring governor %s for cpu %d\n", 790 policy->governor->name, cpu); 791 } 792#endif 793 794 for_each_cpu(j, policy->cpus) { 795 struct cpufreq_policy *managed_policy; 796 797 if (cpu == j) 798 continue; 799 800 /* Check for existing affected CPUs. 801 * They may not be aware of it due to CPU Hotplug. 802 * cpufreq_cpu_put is called when the device is removed 803 * in __cpufreq_remove_dev() 804 */ 805 managed_policy = cpufreq_cpu_get(j); 806 if (unlikely(managed_policy)) { 807 808 /* Set proper policy_cpu */ 809 unlock_policy_rwsem_write(cpu); 810 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->cpu; 811 812 if (lock_policy_rwsem_write(cpu) < 0) { 813 /* Should not go through policy unlock path */ 814 if (cpufreq_driver->exit) 815 cpufreq_driver->exit(policy); 816 cpufreq_cpu_put(managed_policy); 817 return -EBUSY; 818 } 819 820 spin_lock_irqsave(&cpufreq_driver_lock, flags); 821 cpumask_copy(managed_policy->cpus, policy->cpus); 822 per_cpu(cpufreq_cpu_data, cpu) = managed_policy; 823 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 824 825 dprintk("CPU already managed, adding link\n"); 826 ret = sysfs_create_link(&sys_dev->kobj, 827 &managed_policy->kobj, 828 "cpufreq"); 829 if (ret) 830 cpufreq_cpu_put(managed_policy); 831 /* 832 * Success. We only needed to be added to the mask. 833 * Call driver->exit() because only the cpu parent of 834 * the kobj needed to call init(). 835 */ 836 if (cpufreq_driver->exit) 837 cpufreq_driver->exit(policy); 838 839 if (!ret) 840 return 1; 841 else 842 return ret; 843 } 844 } 845#endif 846 return ret; 847} 848 849 850/* symlink affected CPUs */ 851static int cpufreq_add_dev_symlink(unsigned int cpu, 852 struct cpufreq_policy *policy) 853{ 854 unsigned int j; 855 int ret = 0; 856 857 for_each_cpu(j, policy->cpus) { 858 struct cpufreq_policy *managed_policy; 859 struct sys_device *cpu_sys_dev; 860 861 if (j == cpu) 862 continue; 863 if (!cpu_online(j)) 864 continue; 865 866 dprintk("CPU %u already managed, adding link\n", j); 867 managed_policy = cpufreq_cpu_get(cpu); 868 cpu_sys_dev = get_cpu_sysdev(j); 869 ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj, 870 "cpufreq"); 871 if (ret) { 872 cpufreq_cpu_put(managed_policy); 873 return ret; 874 } 875 } 876 return ret; 877} 878 879static int cpufreq_add_dev_interface(unsigned int cpu, 880 struct cpufreq_policy *policy, 881 struct sys_device *sys_dev) 882{ 883 struct cpufreq_policy new_policy; 884 struct freq_attr **drv_attr; 885 unsigned long flags; 886 int ret = 0; 887 unsigned int j; 888 889 /* prepare interface data */ 890 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq, 891 &sys_dev->kobj, "cpufreq"); 892 if (ret) 893 return ret; 894 895 /* set up files for this cpu device */ 896 drv_attr = cpufreq_driver->attr; 897 while ((drv_attr) && (*drv_attr)) { 898 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr)); 899 if (ret) 900 goto err_out_kobj_put; 901 drv_attr++; 902 } 903 if (cpufreq_driver->get) { 904 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr); 905 if (ret) 906 goto err_out_kobj_put; 907 } 908 if (cpufreq_driver->target) { 909 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr); 910 if (ret) 911 goto err_out_kobj_put; 912 } 913 if (cpufreq_driver->bios_limit) { 914 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr); 915 if (ret) 916 goto err_out_kobj_put; 917 } 918 919 spin_lock_irqsave(&cpufreq_driver_lock, flags); 920 for_each_cpu(j, policy->cpus) { 921 if (!cpu_online(j)) 922 continue; 923 per_cpu(cpufreq_cpu_data, j) = policy; 924 per_cpu(cpufreq_policy_cpu, j) = policy->cpu; 925 } 926 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 927 928 ret = cpufreq_add_dev_symlink(cpu, policy); 929 if (ret) 930 goto err_out_kobj_put; 931 932 memcpy(&new_policy, policy, sizeof(struct cpufreq_policy)); 933 /* assure that the starting sequence is run in __cpufreq_set_policy */ 934 policy->governor = NULL; 935 936 /* set default policy */ 937 ret = __cpufreq_set_policy(policy, &new_policy); 938 policy->user_policy.policy = policy->policy; 939 policy->user_policy.governor = policy->governor; 940 941 if (ret) { 942 dprintk("setting policy failed\n"); 943 if (cpufreq_driver->exit) 944 cpufreq_driver->exit(policy); 945 } 946 return ret; 947 948err_out_kobj_put: 949 kobject_put(&policy->kobj); 950 wait_for_completion(&policy->kobj_unregister); 951 return ret; 952} 953 954 955/** 956 * cpufreq_add_dev - add a CPU device 957 * 958 * Adds the cpufreq interface for a CPU device. 959 * 960 * The Oracle says: try running cpufreq registration/unregistration concurrently 961 * with with cpu hotplugging and all hell will break loose. Tried to clean this 962 * mess up, but more thorough testing is needed. - Mathieu 963 */ 964static int cpufreq_add_dev(struct sys_device *sys_dev) 965{ 966 unsigned int cpu = sys_dev->id; 967 int ret = 0, found = 0; 968 struct cpufreq_policy *policy; 969 unsigned long flags; 970 unsigned int j; 971#ifdef CONFIG_HOTPLUG_CPU 972 int sibling; 973#endif 974 975 if (cpu_is_offline(cpu)) 976 return 0; 977 978 cpufreq_debug_disable_ratelimit(); 979 dprintk("adding CPU %u\n", cpu); 980 981#ifdef CONFIG_SMP 982 /* check whether a different CPU already registered this 983 * CPU because it is in the same boat. */ 984 policy = cpufreq_cpu_get(cpu); 985 if (unlikely(policy)) { 986 cpufreq_cpu_put(policy); 987 cpufreq_debug_enable_ratelimit(); 988 return 0; 989 } 990#endif 991 992 if (!try_module_get(cpufreq_driver->owner)) { 993 ret = -EINVAL; 994 goto module_out; 995 } 996 997 ret = -ENOMEM; 998 policy = kzalloc(sizeof(struct cpufreq_policy), GFP_KERNEL); 999 if (!policy) 1000 goto nomem_out; 1001 1002 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL)) 1003 goto err_free_policy; 1004 1005 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL)) 1006 goto err_free_cpumask; 1007 1008 policy->cpu = cpu; 1009 cpumask_copy(policy->cpus, cpumask_of(cpu)); 1010 1011 /* Initially set CPU itself as the policy_cpu */ 1012 per_cpu(cpufreq_policy_cpu, cpu) = cpu; 1013 ret = (lock_policy_rwsem_write(cpu) < 0); 1014 WARN_ON(ret); 1015 1016 init_completion(&policy->kobj_unregister); 1017 INIT_WORK(&policy->update, handle_update); 1018 1019 /* Set governor before ->init, so that driver could check it */ 1020#ifdef CONFIG_HOTPLUG_CPU 1021 for_each_online_cpu(sibling) { 1022 struct cpufreq_policy *cp = per_cpu(cpufreq_cpu_data, sibling); 1023 if (cp && cp->governor && 1024 (cpumask_test_cpu(cpu, cp->related_cpus))) { 1025 policy->governor = cp->governor; 1026 found = 1; 1027 break; 1028 } 1029 } 1030#endif 1031 if (!found) 1032 policy->governor = CPUFREQ_DEFAULT_GOVERNOR; 1033 /* call driver. From then on the cpufreq must be able 1034 * to accept all calls to ->verify and ->setpolicy for this CPU 1035 */ 1036 ret = cpufreq_driver->init(policy); 1037 if (ret) { 1038 dprintk("initialization failed\n"); 1039 goto err_unlock_policy; 1040 } 1041 policy->user_policy.min = policy->min; 1042 policy->user_policy.max = policy->max; 1043 1044 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1045 CPUFREQ_START, policy); 1046 1047 ret = cpufreq_add_dev_policy(cpu, policy, sys_dev); 1048 if (ret) { 1049 if (ret > 0) 1050 /* This is a managed cpu, symlink created, 1051 exit with 0 */ 1052 ret = 0; 1053 goto err_unlock_policy; 1054 } 1055 1056 ret = cpufreq_add_dev_interface(cpu, policy, sys_dev); 1057 if (ret) 1058 goto err_out_unregister; 1059 1060 unlock_policy_rwsem_write(cpu); 1061 1062 kobject_uevent(&policy->kobj, KOBJ_ADD); 1063 module_put(cpufreq_driver->owner); 1064 dprintk("initialization complete\n"); 1065 cpufreq_debug_enable_ratelimit(); 1066 1067 return 0; 1068 1069 1070err_out_unregister: 1071 spin_lock_irqsave(&cpufreq_driver_lock, flags); 1072 for_each_cpu(j, policy->cpus) 1073 per_cpu(cpufreq_cpu_data, j) = NULL; 1074 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1075 1076 kobject_put(&policy->kobj); 1077 wait_for_completion(&policy->kobj_unregister); 1078 1079err_unlock_policy: 1080 unlock_policy_rwsem_write(cpu); 1081 free_cpumask_var(policy->related_cpus); 1082err_free_cpumask: 1083 free_cpumask_var(policy->cpus); 1084err_free_policy: 1085 kfree(policy); 1086nomem_out: 1087 module_put(cpufreq_driver->owner); 1088module_out: 1089 cpufreq_debug_enable_ratelimit(); 1090 return ret; 1091} 1092 1093 1094/** 1095 * __cpufreq_remove_dev - remove a CPU device 1096 * 1097 * Removes the cpufreq interface for a CPU device. 1098 * Caller should already have policy_rwsem in write mode for this CPU. 1099 * This routine frees the rwsem before returning. 1100 */ 1101static int __cpufreq_remove_dev(struct sys_device *sys_dev) 1102{ 1103 unsigned int cpu = sys_dev->id; 1104 unsigned long flags; 1105 struct cpufreq_policy *data; 1106 struct kobject *kobj; 1107 struct completion *cmp; 1108#ifdef CONFIG_SMP 1109 struct sys_device *cpu_sys_dev; 1110 unsigned int j; 1111#endif 1112 1113 cpufreq_debug_disable_ratelimit(); 1114 dprintk("unregistering CPU %u\n", cpu); 1115 1116 spin_lock_irqsave(&cpufreq_driver_lock, flags); 1117 data = per_cpu(cpufreq_cpu_data, cpu); 1118 1119 if (!data) { 1120 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1121 cpufreq_debug_enable_ratelimit(); 1122 unlock_policy_rwsem_write(cpu); 1123 return -EINVAL; 1124 } 1125 per_cpu(cpufreq_cpu_data, cpu) = NULL; 1126 1127 1128#ifdef CONFIG_SMP 1129 /* if this isn't the CPU which is the parent of the kobj, we 1130 * only need to unlink, put and exit 1131 */ 1132 if (unlikely(cpu != data->cpu)) { 1133 dprintk("removing link\n"); 1134 cpumask_clear_cpu(cpu, data->cpus); 1135 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1136 kobj = &sys_dev->kobj; 1137 cpufreq_cpu_put(data); 1138 cpufreq_debug_enable_ratelimit(); 1139 unlock_policy_rwsem_write(cpu); 1140 sysfs_remove_link(kobj, "cpufreq"); 1141 return 0; 1142 } 1143#endif 1144 1145#ifdef CONFIG_SMP 1146 1147#ifdef CONFIG_HOTPLUG_CPU 1148 strncpy(per_cpu(cpufreq_cpu_governor, cpu), data->governor->name, 1149 CPUFREQ_NAME_LEN); 1150#endif 1151 1152 /* if we have other CPUs still registered, we need to unlink them, 1153 * or else wait_for_completion below will lock up. Clean the 1154 * per_cpu(cpufreq_cpu_data) while holding the lock, and remove 1155 * the sysfs links afterwards. 1156 */ 1157 if (unlikely(cpumask_weight(data->cpus) > 1)) { 1158 for_each_cpu(j, data->cpus) { 1159 if (j == cpu) 1160 continue; 1161 per_cpu(cpufreq_cpu_data, j) = NULL; 1162 } 1163 } 1164 1165 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1166 1167 if (unlikely(cpumask_weight(data->cpus) > 1)) { 1168 for_each_cpu(j, data->cpus) { 1169 if (j == cpu) 1170 continue; 1171 dprintk("removing link for cpu %u\n", j); 1172#ifdef CONFIG_HOTPLUG_CPU 1173 strncpy(per_cpu(cpufreq_cpu_governor, j), 1174 data->governor->name, CPUFREQ_NAME_LEN); 1175#endif 1176 cpu_sys_dev = get_cpu_sysdev(j); 1177 kobj = &cpu_sys_dev->kobj; 1178 unlock_policy_rwsem_write(cpu); 1179 sysfs_remove_link(kobj, "cpufreq"); 1180 lock_policy_rwsem_write(cpu); 1181 cpufreq_cpu_put(data); 1182 } 1183 } 1184#else 1185 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1186#endif 1187 1188 if (cpufreq_driver->target) 1189 __cpufreq_governor(data, CPUFREQ_GOV_STOP); 1190 1191 kobj = &data->kobj; 1192 cmp = &data->kobj_unregister; 1193 unlock_policy_rwsem_write(cpu); 1194 kobject_put(kobj); 1195 1196 /* we need to make sure that the underlying kobj is actually 1197 * not referenced anymore by anybody before we proceed with 1198 * unloading. 1199 */ 1200 dprintk("waiting for dropping of refcount\n"); 1201 wait_for_completion(cmp); 1202 dprintk("wait complete\n"); 1203 1204 lock_policy_rwsem_write(cpu); 1205 if (cpufreq_driver->exit) 1206 cpufreq_driver->exit(data); 1207 unlock_policy_rwsem_write(cpu); 1208 1209 free_cpumask_var(data->related_cpus); 1210 free_cpumask_var(data->cpus); 1211 kfree(data); 1212 per_cpu(cpufreq_cpu_data, cpu) = NULL; 1213 1214 cpufreq_debug_enable_ratelimit(); 1215 return 0; 1216} 1217 1218 1219static int cpufreq_remove_dev(struct sys_device *sys_dev) 1220{ 1221 unsigned int cpu = sys_dev->id; 1222 int retval; 1223 1224 if (cpu_is_offline(cpu)) 1225 return 0; 1226 1227 if (unlikely(lock_policy_rwsem_write(cpu))) 1228 BUG(); 1229 1230 retval = __cpufreq_remove_dev(sys_dev); 1231 return retval; 1232} 1233 1234 1235static void handle_update(struct work_struct *work) 1236{ 1237 struct cpufreq_policy *policy = 1238 container_of(work, struct cpufreq_policy, update); 1239 unsigned int cpu = policy->cpu; 1240 dprintk("handle_update for cpu %u called\n", cpu); 1241 cpufreq_update_policy(cpu); 1242} 1243 1244/** 1245 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble. 1246 * @cpu: cpu number 1247 * @old_freq: CPU frequency the kernel thinks the CPU runs at 1248 * @new_freq: CPU frequency the CPU actually runs at 1249 * 1250 * We adjust to current frequency first, and need to clean up later. 1251 * So either call to cpufreq_update_policy() or schedule handle_update()). 1252 */ 1253static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq, 1254 unsigned int new_freq) 1255{ 1256 struct cpufreq_freqs freqs; 1257 1258 dprintk("Warning: CPU frequency out of sync: cpufreq and timing " 1259 "core thinks of %u, is %u kHz.\n", old_freq, new_freq); 1260 1261 freqs.cpu = cpu; 1262 freqs.old = old_freq; 1263 freqs.new = new_freq; 1264 cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE); 1265 cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE); 1266} 1267 1268 1269/** 1270 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur 1271 * @cpu: CPU number 1272 * 1273 * This is the last known freq, without actually getting it from the driver. 1274 * Return value will be same as what is shown in scaling_cur_freq in sysfs. 1275 */ 1276unsigned int cpufreq_quick_get(unsigned int cpu) 1277{ 1278 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1279 unsigned int ret_freq = 0; 1280 1281 if (policy) { 1282 ret_freq = policy->cur; 1283 cpufreq_cpu_put(policy); 1284 } 1285 1286 return ret_freq; 1287} 1288EXPORT_SYMBOL(cpufreq_quick_get); 1289 1290 1291static unsigned int __cpufreq_get(unsigned int cpu) 1292{ 1293 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1294 unsigned int ret_freq = 0; 1295 1296 if (!cpufreq_driver->get) 1297 return ret_freq; 1298 1299 ret_freq = cpufreq_driver->get(cpu); 1300 1301 if (ret_freq && policy->cur && 1302 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) { 1303 /* verify no discrepancy between actual and 1304 saved value exists */ 1305 if (unlikely(ret_freq != policy->cur)) { 1306 cpufreq_out_of_sync(cpu, policy->cur, ret_freq); 1307 schedule_work(&policy->update); 1308 } 1309 } 1310 1311 return ret_freq; 1312} 1313 1314/** 1315 * cpufreq_get - get the current CPU frequency (in kHz) 1316 * @cpu: CPU number 1317 * 1318 * Get the CPU current (static) CPU frequency 1319 */ 1320unsigned int cpufreq_get(unsigned int cpu) 1321{ 1322 unsigned int ret_freq = 0; 1323 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu); 1324 1325 if (!policy) 1326 goto out; 1327 1328 if (unlikely(lock_policy_rwsem_read(cpu))) 1329 goto out_policy; 1330 1331 ret_freq = __cpufreq_get(cpu); 1332 1333 unlock_policy_rwsem_read(cpu); 1334 1335out_policy: 1336 cpufreq_cpu_put(policy); 1337out: 1338 return ret_freq; 1339} 1340EXPORT_SYMBOL(cpufreq_get); 1341 1342 1343/** 1344 * cpufreq_suspend - let the low level driver prepare for suspend 1345 */ 1346 1347static int cpufreq_suspend(struct sys_device *sysdev, pm_message_t pmsg) 1348{ 1349 int ret = 0; 1350 1351 int cpu = sysdev->id; 1352 struct cpufreq_policy *cpu_policy; 1353 1354 dprintk("suspending cpu %u\n", cpu); 1355 1356 if (!cpu_online(cpu)) 1357 return 0; 1358 1359 /* we may be lax here as interrupts are off. Nonetheless 1360 * we need to grab the correct cpu policy, as to check 1361 * whether we really run on this CPU. 1362 */ 1363 1364 cpu_policy = cpufreq_cpu_get(cpu); 1365 if (!cpu_policy) 1366 return -EINVAL; 1367 1368 /* only handle each CPU group once */ 1369 if (unlikely(cpu_policy->cpu != cpu)) 1370 goto out; 1371 1372 if (cpufreq_driver->suspend) { 1373 ret = cpufreq_driver->suspend(cpu_policy, pmsg); 1374 if (ret) 1375 printk(KERN_ERR "cpufreq: suspend failed in ->suspend " 1376 "step on CPU %u\n", cpu_policy->cpu); 1377 } 1378 1379out: 1380 cpufreq_cpu_put(cpu_policy); 1381 return ret; 1382} 1383 1384/** 1385 * cpufreq_resume - restore proper CPU frequency handling after resume 1386 * 1387 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume()) 1388 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are 1389 * restored. It will verify that the current freq is in sync with 1390 * what we believe it to be. This is a bit later than when it 1391 * should be, but nonethteless it's better than calling 1392 * cpufreq_driver->get() here which might re-enable interrupts... 1393 */ 1394static int cpufreq_resume(struct sys_device *sysdev) 1395{ 1396 int ret = 0; 1397 1398 int cpu = sysdev->id; 1399 struct cpufreq_policy *cpu_policy; 1400 1401 dprintk("resuming cpu %u\n", cpu); 1402 1403 if (!cpu_online(cpu)) 1404 return 0; 1405 1406 /* we may be lax here as interrupts are off. Nonetheless 1407 * we need to grab the correct cpu policy, as to check 1408 * whether we really run on this CPU. 1409 */ 1410 1411 cpu_policy = cpufreq_cpu_get(cpu); 1412 if (!cpu_policy) 1413 return -EINVAL; 1414 1415 /* only handle each CPU group once */ 1416 if (unlikely(cpu_policy->cpu != cpu)) 1417 goto fail; 1418 1419 if (cpufreq_driver->resume) { 1420 ret = cpufreq_driver->resume(cpu_policy); 1421 if (ret) { 1422 printk(KERN_ERR "cpufreq: resume failed in ->resume " 1423 "step on CPU %u\n", cpu_policy->cpu); 1424 goto fail; 1425 } 1426 } 1427 1428 schedule_work(&cpu_policy->update); 1429 1430fail: 1431 cpufreq_cpu_put(cpu_policy); 1432 return ret; 1433} 1434 1435static struct sysdev_driver cpufreq_sysdev_driver = { 1436 .add = cpufreq_add_dev, 1437 .remove = cpufreq_remove_dev, 1438 .suspend = cpufreq_suspend, 1439 .resume = cpufreq_resume, 1440}; 1441 1442 1443/********************************************************************* 1444 * NOTIFIER LISTS INTERFACE * 1445 *********************************************************************/ 1446 1447/** 1448 * cpufreq_register_notifier - register a driver with cpufreq 1449 * @nb: notifier function to register 1450 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 1451 * 1452 * Add a driver to one of two lists: either a list of drivers that 1453 * are notified about clock rate changes (once before and once after 1454 * the transition), or a list of drivers that are notified about 1455 * changes in cpufreq policy. 1456 * 1457 * This function may sleep, and has the same return conditions as 1458 * blocking_notifier_chain_register. 1459 */ 1460int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list) 1461{ 1462 int ret; 1463 1464 WARN_ON(!init_cpufreq_transition_notifier_list_called); 1465 1466 switch (list) { 1467 case CPUFREQ_TRANSITION_NOTIFIER: 1468 ret = srcu_notifier_chain_register( 1469 &cpufreq_transition_notifier_list, nb); 1470 break; 1471 case CPUFREQ_POLICY_NOTIFIER: 1472 ret = blocking_notifier_chain_register( 1473 &cpufreq_policy_notifier_list, nb); 1474 break; 1475 default: 1476 ret = -EINVAL; 1477 } 1478 1479 return ret; 1480} 1481EXPORT_SYMBOL(cpufreq_register_notifier); 1482 1483 1484/** 1485 * cpufreq_unregister_notifier - unregister a driver with cpufreq 1486 * @nb: notifier block to be unregistered 1487 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER 1488 * 1489 * Remove a driver from the CPU frequency notifier list. 1490 * 1491 * This function may sleep, and has the same return conditions as 1492 * blocking_notifier_chain_unregister. 1493 */ 1494int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list) 1495{ 1496 int ret; 1497 1498 switch (list) { 1499 case CPUFREQ_TRANSITION_NOTIFIER: 1500 ret = srcu_notifier_chain_unregister( 1501 &cpufreq_transition_notifier_list, nb); 1502 break; 1503 case CPUFREQ_POLICY_NOTIFIER: 1504 ret = blocking_notifier_chain_unregister( 1505 &cpufreq_policy_notifier_list, nb); 1506 break; 1507 default: 1508 ret = -EINVAL; 1509 } 1510 1511 return ret; 1512} 1513EXPORT_SYMBOL(cpufreq_unregister_notifier); 1514 1515 1516/********************************************************************* 1517 * GOVERNORS * 1518 *********************************************************************/ 1519 1520 1521int __cpufreq_driver_target(struct cpufreq_policy *policy, 1522 unsigned int target_freq, 1523 unsigned int relation) 1524{ 1525 int retval = -EINVAL; 1526 1527 dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu, 1528 target_freq, relation); 1529 if (cpu_online(policy->cpu) && cpufreq_driver->target) 1530 retval = cpufreq_driver->target(policy, target_freq, relation); 1531 1532 return retval; 1533} 1534EXPORT_SYMBOL_GPL(__cpufreq_driver_target); 1535 1536int cpufreq_driver_target(struct cpufreq_policy *policy, 1537 unsigned int target_freq, 1538 unsigned int relation) 1539{ 1540 int ret = -EINVAL; 1541 1542 policy = cpufreq_cpu_get(policy->cpu); 1543 if (!policy) 1544 goto no_policy; 1545 1546 if (unlikely(lock_policy_rwsem_write(policy->cpu))) 1547 goto fail; 1548 1549 ret = __cpufreq_driver_target(policy, target_freq, relation); 1550 1551 unlock_policy_rwsem_write(policy->cpu); 1552 1553fail: 1554 cpufreq_cpu_put(policy); 1555no_policy: 1556 return ret; 1557} 1558EXPORT_SYMBOL_GPL(cpufreq_driver_target); 1559 1560int __cpufreq_driver_getavg(struct cpufreq_policy *policy, unsigned int cpu) 1561{ 1562 int ret = 0; 1563 1564 policy = cpufreq_cpu_get(policy->cpu); 1565 if (!policy) 1566 return -EINVAL; 1567 1568 if (cpu_online(cpu) && cpufreq_driver->getavg) 1569 ret = cpufreq_driver->getavg(policy, cpu); 1570 1571 cpufreq_cpu_put(policy); 1572 return ret; 1573} 1574EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg); 1575 1576/* 1577 * when "event" is CPUFREQ_GOV_LIMITS 1578 */ 1579 1580static int __cpufreq_governor(struct cpufreq_policy *policy, 1581 unsigned int event) 1582{ 1583 int ret; 1584 1585 /* Only must be defined when default governor is known to have latency 1586 restrictions, like e.g. conservative or ondemand. 1587 That this is the case is already ensured in Kconfig 1588 */ 1589#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE 1590 struct cpufreq_governor *gov = &cpufreq_gov_performance; 1591#else 1592 struct cpufreq_governor *gov = NULL; 1593#endif 1594 1595 if (policy->governor->max_transition_latency && 1596 policy->cpuinfo.transition_latency > 1597 policy->governor->max_transition_latency) { 1598 if (!gov) 1599 return -EINVAL; 1600 else { 1601 printk(KERN_WARNING "%s governor failed, too long" 1602 " transition latency of HW, fallback" 1603 " to %s governor\n", 1604 policy->governor->name, 1605 gov->name); 1606 policy->governor = gov; 1607 } 1608 } 1609 1610 if (!try_module_get(policy->governor->owner)) 1611 return -EINVAL; 1612 1613 dprintk("__cpufreq_governor for CPU %u, event %u\n", 1614 policy->cpu, event); 1615 ret = policy->governor->governor(policy, event); 1616 1617 /* we keep one module reference alive for 1618 each CPU governed by this CPU */ 1619 if ((event != CPUFREQ_GOV_START) || ret) 1620 module_put(policy->governor->owner); 1621 if ((event == CPUFREQ_GOV_STOP) && !ret) 1622 module_put(policy->governor->owner); 1623 1624 return ret; 1625} 1626 1627 1628int cpufreq_register_governor(struct cpufreq_governor *governor) 1629{ 1630 int err; 1631 1632 if (!governor) 1633 return -EINVAL; 1634 1635 mutex_lock(&cpufreq_governor_mutex); 1636 1637 err = -EBUSY; 1638 if (__find_governor(governor->name) == NULL) { 1639 err = 0; 1640 list_add(&governor->governor_list, &cpufreq_governor_list); 1641 } 1642 1643 mutex_unlock(&cpufreq_governor_mutex); 1644 return err; 1645} 1646EXPORT_SYMBOL_GPL(cpufreq_register_governor); 1647 1648 1649void cpufreq_unregister_governor(struct cpufreq_governor *governor) 1650{ 1651#ifdef CONFIG_HOTPLUG_CPU 1652 int cpu; 1653#endif 1654 1655 if (!governor) 1656 return; 1657 1658#ifdef CONFIG_HOTPLUG_CPU 1659 for_each_present_cpu(cpu) { 1660 if (cpu_online(cpu)) 1661 continue; 1662 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name)) 1663 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0"); 1664 } 1665#endif 1666 1667 mutex_lock(&cpufreq_governor_mutex); 1668 list_del(&governor->governor_list); 1669 mutex_unlock(&cpufreq_governor_mutex); 1670 return; 1671} 1672EXPORT_SYMBOL_GPL(cpufreq_unregister_governor); 1673 1674 1675 1676/********************************************************************* 1677 * POLICY INTERFACE * 1678 *********************************************************************/ 1679 1680/** 1681 * cpufreq_get_policy - get the current cpufreq_policy 1682 * @policy: struct cpufreq_policy into which the current cpufreq_policy 1683 * is written 1684 * 1685 * Reads the current cpufreq policy. 1686 */ 1687int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu) 1688{ 1689 struct cpufreq_policy *cpu_policy; 1690 if (!policy) 1691 return -EINVAL; 1692 1693 cpu_policy = cpufreq_cpu_get(cpu); 1694 if (!cpu_policy) 1695 return -EINVAL; 1696 1697 memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy)); 1698 1699 cpufreq_cpu_put(cpu_policy); 1700 return 0; 1701} 1702EXPORT_SYMBOL(cpufreq_get_policy); 1703 1704 1705/* 1706 * data : current policy. 1707 * policy : policy to be set. 1708 */ 1709static int __cpufreq_set_policy(struct cpufreq_policy *data, 1710 struct cpufreq_policy *policy) 1711{ 1712 int ret = 0; 1713 1714 cpufreq_debug_disable_ratelimit(); 1715 dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu, 1716 policy->min, policy->max); 1717 1718 memcpy(&policy->cpuinfo, &data->cpuinfo, 1719 sizeof(struct cpufreq_cpuinfo)); 1720 1721 if (policy->min > data->max || policy->max < data->min) { 1722 ret = -EINVAL; 1723 goto error_out; 1724 } 1725 1726 /* verify the cpu speed can be set within this limit */ 1727 ret = cpufreq_driver->verify(policy); 1728 if (ret) 1729 goto error_out; 1730 1731 /* adjust if necessary - all reasons */ 1732 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1733 CPUFREQ_ADJUST, policy); 1734 1735 /* adjust if necessary - hardware incompatibility*/ 1736 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1737 CPUFREQ_INCOMPATIBLE, policy); 1738 1739 /* verify the cpu speed can be set within this limit, 1740 which might be different to the first one */ 1741 ret = cpufreq_driver->verify(policy); 1742 if (ret) 1743 goto error_out; 1744 1745 /* notification of the new policy */ 1746 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1747 CPUFREQ_NOTIFY, policy); 1748 1749 data->min = policy->min; 1750 data->max = policy->max; 1751 1752 dprintk("new min and max freqs are %u - %u kHz\n", 1753 data->min, data->max); 1754 1755 if (cpufreq_driver->setpolicy) { 1756 data->policy = policy->policy; 1757 dprintk("setting range\n"); 1758 ret = cpufreq_driver->setpolicy(policy); 1759 } else { 1760 if (policy->governor != data->governor) { 1761 /* save old, working values */ 1762 struct cpufreq_governor *old_gov = data->governor; 1763 1764 dprintk("governor switch\n"); 1765 1766 /* end old governor */ 1767 if (data->governor) 1768 __cpufreq_governor(data, CPUFREQ_GOV_STOP); 1769 1770 /* start new governor */ 1771 data->governor = policy->governor; 1772 if (__cpufreq_governor(data, CPUFREQ_GOV_START)) { 1773 /* new governor failed, so re-start old one */ 1774 dprintk("starting governor %s failed\n", 1775 data->governor->name); 1776 if (old_gov) { 1777 data->governor = old_gov; 1778 __cpufreq_governor(data, 1779 CPUFREQ_GOV_START); 1780 } 1781 ret = -EINVAL; 1782 goto error_out; 1783 } 1784 /* might be a policy change, too, so fall through */ 1785 } 1786 dprintk("governor: change or update limits\n"); 1787 __cpufreq_governor(data, CPUFREQ_GOV_LIMITS); 1788 } 1789 1790error_out: 1791 cpufreq_debug_enable_ratelimit(); 1792 return ret; 1793} 1794 1795/** 1796 * cpufreq_update_policy - re-evaluate an existing cpufreq policy 1797 * @cpu: CPU which shall be re-evaluated 1798 * 1799 * Usefull for policy notifiers which have different necessities 1800 * at different times. 1801 */ 1802int cpufreq_update_policy(unsigned int cpu) 1803{ 1804 struct cpufreq_policy *data = cpufreq_cpu_get(cpu); 1805 struct cpufreq_policy policy; 1806 int ret; 1807 1808 if (!data) { 1809 ret = -ENODEV; 1810 goto no_policy; 1811 } 1812 1813 if (unlikely(lock_policy_rwsem_write(cpu))) { 1814 ret = -EINVAL; 1815 goto fail; 1816 } 1817 1818 dprintk("updating policy for CPU %u\n", cpu); 1819 memcpy(&policy, data, sizeof(struct cpufreq_policy)); 1820 policy.min = data->user_policy.min; 1821 policy.max = data->user_policy.max; 1822 policy.policy = data->user_policy.policy; 1823 policy.governor = data->user_policy.governor; 1824 1825 /* BIOS might change freq behind our back 1826 -> ask driver for current freq and notify governors about a change */ 1827 if (cpufreq_driver->get) { 1828 policy.cur = cpufreq_driver->get(cpu); 1829 if (!data->cur) { 1830 dprintk("Driver did not initialize current freq"); 1831 data->cur = policy.cur; 1832 } else { 1833 if (data->cur != policy.cur) 1834 cpufreq_out_of_sync(cpu, data->cur, 1835 policy.cur); 1836 } 1837 } 1838 1839 ret = __cpufreq_set_policy(data, &policy); 1840 1841 unlock_policy_rwsem_write(cpu); 1842 1843fail: 1844 cpufreq_cpu_put(data); 1845no_policy: 1846 return ret; 1847} 1848EXPORT_SYMBOL(cpufreq_update_policy); 1849 1850static int __cpuinit cpufreq_cpu_callback(struct notifier_block *nfb, 1851 unsigned long action, void *hcpu) 1852{ 1853 unsigned int cpu = (unsigned long)hcpu; 1854 struct sys_device *sys_dev; 1855 1856 sys_dev = get_cpu_sysdev(cpu); 1857 if (sys_dev) { 1858 switch (action) { 1859 case CPU_ONLINE: 1860 case CPU_ONLINE_FROZEN: 1861 cpufreq_add_dev(sys_dev); 1862 break; 1863 case CPU_DOWN_PREPARE: 1864 case CPU_DOWN_PREPARE_FROZEN: 1865 if (unlikely(lock_policy_rwsem_write(cpu))) 1866 BUG(); 1867 1868 __cpufreq_remove_dev(sys_dev); 1869 break; 1870 case CPU_DOWN_FAILED: 1871 case CPU_DOWN_FAILED_FROZEN: 1872 cpufreq_add_dev(sys_dev); 1873 break; 1874 } 1875 } 1876 return NOTIFY_OK; 1877} 1878 1879static struct notifier_block __refdata cpufreq_cpu_notifier = { 1880 .notifier_call = cpufreq_cpu_callback, 1881}; 1882 1883/********************************************************************* 1884 * REGISTER / UNREGISTER CPUFREQ DRIVER * 1885 *********************************************************************/ 1886 1887/** 1888 * cpufreq_register_driver - register a CPU Frequency driver 1889 * @driver_data: A struct cpufreq_driver containing the values# 1890 * submitted by the CPU Frequency driver. 1891 * 1892 * Registers a CPU Frequency driver to this core code. This code 1893 * returns zero on success, -EBUSY when another driver got here first 1894 * (and isn't unregistered in the meantime). 1895 * 1896 */ 1897int cpufreq_register_driver(struct cpufreq_driver *driver_data) 1898{ 1899 unsigned long flags; 1900 int ret; 1901 1902 if (!driver_data || !driver_data->verify || !driver_data->init || 1903 ((!driver_data->setpolicy) && (!driver_data->target))) 1904 return -EINVAL; 1905 1906 dprintk("trying to register driver %s\n", driver_data->name); 1907 1908 if (driver_data->setpolicy) 1909 driver_data->flags |= CPUFREQ_CONST_LOOPS; 1910 1911 spin_lock_irqsave(&cpufreq_driver_lock, flags); 1912 if (cpufreq_driver) { 1913 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1914 return -EBUSY; 1915 } 1916 cpufreq_driver = driver_data; 1917 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1918 1919 ret = sysdev_driver_register(&cpu_sysdev_class, 1920 &cpufreq_sysdev_driver); 1921 1922 if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) { 1923 int i; 1924 ret = -ENODEV; 1925 1926 /* check for at least one working CPU */ 1927 for (i = 0; i < nr_cpu_ids; i++) 1928 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) { 1929 ret = 0; 1930 break; 1931 } 1932 1933 /* if all ->init() calls failed, unregister */ 1934 if (ret) { 1935 dprintk("no CPU initialized for driver %s\n", 1936 driver_data->name); 1937 sysdev_driver_unregister(&cpu_sysdev_class, 1938 &cpufreq_sysdev_driver); 1939 1940 spin_lock_irqsave(&cpufreq_driver_lock, flags); 1941 cpufreq_driver = NULL; 1942 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1943 } 1944 } 1945 1946 if (!ret) { 1947 register_hotcpu_notifier(&cpufreq_cpu_notifier); 1948 dprintk("driver %s up and running\n", driver_data->name); 1949 cpufreq_debug_enable_ratelimit(); 1950 } 1951 1952 return ret; 1953} 1954EXPORT_SYMBOL_GPL(cpufreq_register_driver); 1955 1956 1957/** 1958 * cpufreq_unregister_driver - unregister the current CPUFreq driver 1959 * 1960 * Unregister the current CPUFreq driver. Only call this if you have 1961 * the right to do so, i.e. if you have succeeded in initialising before! 1962 * Returns zero if successful, and -EINVAL if the cpufreq_driver is 1963 * currently not initialised. 1964 */ 1965int cpufreq_unregister_driver(struct cpufreq_driver *driver) 1966{ 1967 unsigned long flags; 1968 1969 cpufreq_debug_disable_ratelimit(); 1970 1971 if (!cpufreq_driver || (driver != cpufreq_driver)) { 1972 cpufreq_debug_enable_ratelimit(); 1973 return -EINVAL; 1974 } 1975 1976 dprintk("unregistering driver %s\n", driver->name); 1977 1978 sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver); 1979 unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 1980 1981 spin_lock_irqsave(&cpufreq_driver_lock, flags); 1982 cpufreq_driver = NULL; 1983 spin_unlock_irqrestore(&cpufreq_driver_lock, flags); 1984 1985 return 0; 1986} 1987EXPORT_SYMBOL_GPL(cpufreq_unregister_driver); 1988 1989static int __init cpufreq_core_init(void) 1990{ 1991 int cpu; 1992 1993 for_each_possible_cpu(cpu) { 1994 per_cpu(cpufreq_policy_cpu, cpu) = -1; 1995 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu)); 1996 } 1997 1998 cpufreq_global_kobject = kobject_create_and_add("cpufreq", 1999 &cpu_sysdev_class.kset.kobj); 2000 BUG_ON(!cpufreq_global_kobject); 2001 2002 return 0; 2003} 2004core_initcall(cpufreq_core_init); 2005