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