cpufreq.c 66.8 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3 4 5
/*
 *  linux/drivers/cpufreq/cpufreq.c
 *
 *  Copyright (C) 2001 Russell King
 *            (C) 2002 - 2003 Dominik Brodowski <linux@brodo.de>
6
 *            (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
Linus Torvalds's avatar
Linus Torvalds committed
7
 *
8
 *  Oct 2005 - Ashok Raj <ashok.raj@intel.com>
9
 *	Added handling for CPU hotplug
10 11
 *  Feb 2006 - Jacob Shin <jacob.shin@amd.com>
 *	Fix handling for CPU hotplug -- affected CPUs
12
 *
Linus Torvalds's avatar
Linus Torvalds committed
13 14 15 16 17
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

20
#include <linux/cpu.h>
Linus Torvalds's avatar
Linus Torvalds committed
21 22 23
#include <linux/cpufreq.h>
#include <linux/delay.h>
#include <linux/device.h>
24 25 26
#include <linux/init.h>
#include <linux/kernel_stat.h>
#include <linux/module.h>
27
#include <linux/mutex.h>
28
#include <linux/slab.h>
29
#include <linux/suspend.h>
30
#include <linux/syscore_ops.h>
31
#include <linux/tick.h>
32 33
#include <trace/events/power.h>

34
static LIST_HEAD(cpufreq_policy_list);
35 36 37 38 39 40 41

static inline bool policy_is_inactive(struct cpufreq_policy *policy)
{
	return cpumask_empty(policy->cpus);
}

/* Macros to iterate over CPU policies */
42 43 44
#define for_each_suitable_policy(__policy, __active)			 \
	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list) \
		if ((__active) == !policy_is_inactive(__policy))
45 46 47 48 49 50 51

#define for_each_active_policy(__policy)		\
	for_each_suitable_policy(__policy, true)
#define for_each_inactive_policy(__policy)		\
	for_each_suitable_policy(__policy, false)

#define for_each_policy(__policy)			\
52 53
	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)

54 55 56 57 58
/* Iterate over governors */
static LIST_HEAD(cpufreq_governor_list);
#define for_each_governor(__governor)				\
	list_for_each_entry(__governor, &cpufreq_governor_list, governor_list)

Linus Torvalds's avatar
Linus Torvalds committed
59
/**
Dave Jones's avatar
Dave Jones committed
60
 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds's avatar
Linus Torvalds committed
61 62 63
 * level driver of CPUFreq support, and its spinlock. This lock
 * also protects the cpufreq_cpu_data array.
 */
64
static struct cpufreq_driver *cpufreq_driver;
65
static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
66 67
static DEFINE_RWLOCK(cpufreq_driver_lock);

68 69
/* Flag to suspend/resume CPUFreq governors */
static bool cpufreq_suspended;
Linus Torvalds's avatar
Linus Torvalds committed
70

71 72 73 74 75
static inline bool has_target(void)
{
	return cpufreq_driver->target_index || cpufreq_driver->target;
}

Linus Torvalds's avatar
Linus Torvalds committed
76
/* internal prototypes */
77
static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
78 79
static int cpufreq_init_governor(struct cpufreq_policy *policy);
static void cpufreq_exit_governor(struct cpufreq_policy *policy);
80
static int cpufreq_start_governor(struct cpufreq_policy *policy);
81 82
static void cpufreq_stop_governor(struct cpufreq_policy *policy);
static void cpufreq_governor_limits(struct cpufreq_policy *policy);
83

Linus Torvalds's avatar
Linus Torvalds committed
84
/**
85 86
 * Two notifier lists: the "policy" list is involved in the
 * validation process for a new CPU frequency policy; the
Linus Torvalds's avatar
Linus Torvalds committed
87 88 89 90
 * "transition" list for kernel code that needs to handle
 * changes to devices when the CPU clock speed changes.
 * The mutex locks both lists.
 */
91
static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
92
static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds's avatar
Linus Torvalds committed
93

94
static bool init_cpufreq_transition_notifier_list_called;
95 96 97
static int __init init_cpufreq_transition_notifier_list(void)
{
	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
98
	init_cpufreq_transition_notifier_list_called = true;
99 100
	return 0;
}
101
pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds's avatar
Linus Torvalds committed
102

103
static int off __read_mostly;
104
static int cpufreq_disabled(void)
105 106 107 108 109 110 111
{
	return off;
}
void disable_cpufreq(void)
{
	off = 1;
}
112
static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
113

114 115
bool have_governor_per_policy(void)
{
116
	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
117
}
118
EXPORT_SYMBOL_GPL(have_governor_per_policy);
119

120 121 122 123 124 125 126 127 128
struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
{
	if (have_governor_per_policy())
		return &policy->kobj;
	else
		return cpufreq_global_kobject;
}
EXPORT_SYMBOL_GPL(get_governor_parent_kobj);

129 130 131 132 133 134
static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
{
	u64 idle_time;
	u64 cur_wall_time;
	u64 busy_time;

135
	cur_wall_time = jiffies64_to_nsecs(get_jiffies_64());
136 137 138 139 140 141 142 143 144 145

	busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
	busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];

	idle_time = cur_wall_time - busy_time;
	if (wall)
146
		*wall = div_u64(cur_wall_time, NSEC_PER_USEC);
147

148
	return div_u64(idle_time, NSEC_PER_USEC);
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
}

u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
{
	u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);

	if (idle_time == -1ULL)
		return get_cpu_idle_time_jiffy(cpu, wall);
	else if (!io_busy)
		idle_time += get_cpu_iowait_time_us(cpu, wall);

	return idle_time;
}
EXPORT_SYMBOL_GPL(get_cpu_idle_time);

164 165 166 167 168 169
__weak void arch_set_freq_scale(struct cpumask *cpus, unsigned long cur_freq,
		unsigned long max_freq)
{
}
EXPORT_SYMBOL_GPL(arch_set_freq_scale);

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/*
 * This is a generic cpufreq init() routine which can be used by cpufreq
 * drivers of SMP systems. It will do following:
 * - validate & show freq table passed
 * - set policies transition latency
 * - policy->cpus with all possible CPUs
 */
int cpufreq_generic_init(struct cpufreq_policy *policy,
		struct cpufreq_frequency_table *table,
		unsigned int transition_latency)
{
	int ret;

	ret = cpufreq_table_validate_and_show(policy, table);
	if (ret) {
		pr_err("%s: invalid frequency table: %d\n", __func__, ret);
		return ret;
	}

	policy->cpuinfo.transition_latency = transition_latency;

	/*
192
	 * The driver only supports the SMP configuration where all processors
193 194 195 196 197 198 199 200
	 * share the clock and voltage and clock.
	 */
	cpumask_setall(policy->cpus);

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_generic_init);

201
struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
202 203 204
{
	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);

205 206
	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
}
207
EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
208 209 210 211 212

unsigned int cpufreq_generic_get(unsigned int cpu)
{
	struct cpufreq_policy *policy = cpufreq_cpu_get_raw(cpu);

213
	if (!policy || IS_ERR(policy->clk)) {
214 215
		pr_err("%s: No %s associated to cpu: %d\n",
		       __func__, policy ? "clk" : "policy", cpu);
216 217 218 219 220 221 222
		return 0;
	}

	return clk_get_rate(policy->clk) / 1000;
}
EXPORT_SYMBOL_GPL(cpufreq_generic_get);

223 224 225 226 227 228 229 230 231 232 233 234 235
/**
 * cpufreq_cpu_get: returns policy for a cpu and marks it busy.
 *
 * @cpu: cpu to find policy for.
 *
 * This returns policy for 'cpu', returns NULL if it doesn't exist.
 * It also increments the kobject reference count to mark it busy and so would
 * require a corresponding call to cpufreq_cpu_put() to decrement it back.
 * If corresponding call cpufreq_cpu_put() isn't made, the policy wouldn't be
 * freed as that depends on the kobj count.
 *
 * Return: A valid policy on success, otherwise NULL on failure.
 */
236
struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds's avatar
Linus Torvalds committed
237
{
238
	struct cpufreq_policy *policy = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
239 240
	unsigned long flags;

241
	if (WARN_ON(cpu >= nr_cpu_ids))
242 243
		return NULL;

Linus Torvalds's avatar
Linus Torvalds committed
244
	/* get the cpufreq driver */
245
	read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
246

247 248
	if (cpufreq_driver) {
		/* get the CPU */
249
		policy = cpufreq_cpu_get_raw(cpu);
250 251 252
		if (policy)
			kobject_get(&policy->kobj);
	}
Linus Torvalds's avatar
Linus Torvalds committed
253

254
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
255

256
	return policy;
257
}
Linus Torvalds's avatar
Linus Torvalds committed
258 259
EXPORT_SYMBOL_GPL(cpufreq_cpu_get);

260 261 262 263 264 265 266 267
/**
 * cpufreq_cpu_put: Decrements the usage count of a policy
 *
 * @policy: policy earlier returned by cpufreq_cpu_get().
 *
 * This decrements the kobject reference count incremented earlier by calling
 * cpufreq_cpu_get().
 */
268
void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds's avatar
Linus Torvalds committed
269
{
270
	kobject_put(&policy->kobj);
Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274 275 276 277 278 279 280 281 282
}
EXPORT_SYMBOL_GPL(cpufreq_cpu_put);

/*********************************************************************
 *            EXTERNALLY AFFECTING FREQUENCY CHANGES                 *
 *********************************************************************/

/**
 * adjust_jiffies - adjust the system "loops_per_jiffy"
 *
 * This function alters the system "loops_per_jiffy" for the clock
 * speed change. Note that loops_per_jiffy cannot be updated on SMP
283
 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds's avatar
Linus Torvalds committed
284 285
 * per-CPU loops_per_jiffy value wherever possible.
 */
286
static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds's avatar
Linus Torvalds committed
287
{
288 289 290 291
#ifndef CONFIG_SMP
	static unsigned long l_p_j_ref;
	static unsigned int l_p_j_ref_freq;

Linus Torvalds's avatar
Linus Torvalds committed
292 293 294 295 296 297
	if (ci->flags & CPUFREQ_CONST_LOOPS)
		return;

	if (!l_p_j_ref_freq) {
		l_p_j_ref = loops_per_jiffy;
		l_p_j_ref_freq = ci->old;
298 299
		pr_debug("saving %lu as reference value for loops_per_jiffy; freq is %u kHz\n",
			 l_p_j_ref, l_p_j_ref_freq);
Linus Torvalds's avatar
Linus Torvalds committed
300
	}
301
	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
302 303
		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
								ci->new);
304 305
		pr_debug("scaling loops_per_jiffy to %lu for frequency %u kHz\n",
			 loops_per_jiffy, ci->new);
Linus Torvalds's avatar
Linus Torvalds committed
306 307
	}
#endif
308
}
Linus Torvalds's avatar
Linus Torvalds committed
309

310
static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
311
		struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds's avatar
Linus Torvalds committed
312 313 314
{
	BUG_ON(irqs_disabled());

315 316 317
	if (cpufreq_disabled())
		return;

318
	freqs->flags = cpufreq_driver->flags;
319
	pr_debug("notification %u of frequency transition to %u kHz\n",
320
		 state, freqs->new);
Linus Torvalds's avatar
Linus Torvalds committed
321 322

	switch (state) {
323

Linus Torvalds's avatar
Linus Torvalds committed
324
	case CPUFREQ_PRECHANGE:
325
		/* detect if the driver reported a value as "old frequency"
326 327
		 * which is not equal to what the cpufreq core thinks is
		 * "old frequency".
Linus Torvalds's avatar
Linus Torvalds committed
328
		 */
329
		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
330 331
			if ((policy) && (policy->cpu == freqs->cpu) &&
			    (policy->cur) && (policy->cur != freqs->old)) {
332 333
				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
					 freqs->old, policy->cur);
334
				freqs->old = policy->cur;
Linus Torvalds's avatar
Linus Torvalds committed
335 336
			}
		}
337
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
338
				CPUFREQ_PRECHANGE, freqs);
Linus Torvalds's avatar
Linus Torvalds committed
339 340
		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
		break;
341

Linus Torvalds's avatar
Linus Torvalds committed
342 343
	case CPUFREQ_POSTCHANGE:
		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
344 345
		pr_debug("FREQ: %lu - CPU: %lu\n",
			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
346
		trace_cpu_frequency(freqs->new, freqs->cpu);
347
		cpufreq_stats_record_transition(policy, freqs->new);
348
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
349
				CPUFREQ_POSTCHANGE, freqs);
350 351
		if (likely(policy) && likely(policy->cpu == freqs->cpu))
			policy->cur = freqs->new;
Linus Torvalds's avatar
Linus Torvalds committed
352 353 354
		break;
	}
}
355

356 357 358 359 360 361 362 363
/**
 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
 * on frequency transition.
 *
 * This function calls the transition notifiers and the "adjust_jiffies"
 * function. It is called twice on all CPU frequency changes that have
 * external effects.
 */
364
static void cpufreq_notify_transition(struct cpufreq_policy *policy,
365 366 367 368 369
		struct cpufreq_freqs *freqs, unsigned int state)
{
	for_each_cpu(freqs->cpu, policy->cpus)
		__cpufreq_notify_transition(policy, freqs, state);
}
Linus Torvalds's avatar
Linus Torvalds committed
370

371
/* Do post notifications when there are chances that transition has failed */
372
static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
373 374 375 376 377 378 379 380 381 382 383
		struct cpufreq_freqs *freqs, int transition_failed)
{
	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
	if (!transition_failed)
		return;

	swap(freqs->old, freqs->new);
	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
	cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
}

384 385 386
void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
		struct cpufreq_freqs *freqs)
{
387 388 389 390 391 392 393 394 395 396 397 398

	/*
	 * Catch double invocations of _begin() which lead to self-deadlock.
	 * ASYNC_NOTIFICATION drivers are left out because the cpufreq core
	 * doesn't invoke _begin() on their behalf, and hence the chances of
	 * double invocations are very low. Moreover, there are scenarios
	 * where these checks can emit false-positive warnings in these
	 * drivers; so we avoid that by skipping them altogether.
	 */
	WARN_ON(!(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION)
				&& current == policy->transition_task);

399 400 401 402 403 404 405 406 407 408 409
wait:
	wait_event(policy->transition_wait, !policy->transition_ongoing);

	spin_lock(&policy->transition_lock);

	if (unlikely(policy->transition_ongoing)) {
		spin_unlock(&policy->transition_lock);
		goto wait;
	}

	policy->transition_ongoing = true;
410
	policy->transition_task = current;
411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426

	spin_unlock(&policy->transition_lock);

	cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
}
EXPORT_SYMBOL_GPL(cpufreq_freq_transition_begin);

void cpufreq_freq_transition_end(struct cpufreq_policy *policy,
		struct cpufreq_freqs *freqs, int transition_failed)
{
	if (unlikely(WARN_ON(!policy->transition_ongoing)))
		return;

	cpufreq_notify_post_transition(policy, freqs, transition_failed);

	policy->transition_ongoing = false;
427
	policy->transition_task = NULL;
428 429 430 431 432

	wake_up(&policy->transition_wait);
}
EXPORT_SYMBOL_GPL(cpufreq_freq_transition_end);

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
/*
 * Fast frequency switching status count.  Positive means "enabled", negative
 * means "disabled" and 0 means "not decided yet".
 */
static int cpufreq_fast_switch_count;
static DEFINE_MUTEX(cpufreq_fast_switch_lock);

static void cpufreq_list_transition_notifiers(void)
{
	struct notifier_block *nb;

	pr_info("Registered transition notifiers:\n");

	mutex_lock(&cpufreq_transition_notifier_list.mutex);

	for (nb = cpufreq_transition_notifier_list.head; nb; nb = nb->next)
		pr_info("%pF\n", nb->notifier_call);

	mutex_unlock(&cpufreq_transition_notifier_list.mutex);
}

/**
 * cpufreq_enable_fast_switch - Enable fast frequency switching for policy.
 * @policy: cpufreq policy to enable fast frequency switching for.
 *
 * Try to enable fast frequency switching for @policy.
 *
 * The attempt will fail if there is at least one transition notifier registered
 * at this point, as fast frequency switching is quite fundamentally at odds
 * with transition notifiers.  Thus if successful, it will make registration of
 * transition notifiers fail going forward.
 */
void cpufreq_enable_fast_switch(struct cpufreq_policy *policy)
{
	lockdep_assert_held(&policy->rwsem);

	if (!policy->fast_switch_possible)
		return;

	mutex_lock(&cpufreq_fast_switch_lock);
	if (cpufreq_fast_switch_count >= 0) {
		cpufreq_fast_switch_count++;
		policy->fast_switch_enabled = true;
	} else {
		pr_warn("CPU%u: Fast frequency switching not enabled\n",
			policy->cpu);
		cpufreq_list_transition_notifiers();
	}
	mutex_unlock(&cpufreq_fast_switch_lock);
}
EXPORT_SYMBOL_GPL(cpufreq_enable_fast_switch);

485 486 487 488 489
/**
 * cpufreq_disable_fast_switch - Disable fast frequency switching for policy.
 * @policy: cpufreq policy to disable fast frequency switching for.
 */
void cpufreq_disable_fast_switch(struct cpufreq_policy *policy)
490 491 492 493 494 495 496 497 498
{
	mutex_lock(&cpufreq_fast_switch_lock);
	if (policy->fast_switch_enabled) {
		policy->fast_switch_enabled = false;
		if (!WARN_ON(cpufreq_fast_switch_count <= 0))
			cpufreq_fast_switch_count--;
	}
	mutex_unlock(&cpufreq_fast_switch_lock);
}
499
EXPORT_SYMBOL_GPL(cpufreq_disable_fast_switch);
Linus Torvalds's avatar
Linus Torvalds committed
500

501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
/**
 * cpufreq_driver_resolve_freq - Map a target frequency to a driver-supported
 * one.
 * @target_freq: target frequency to resolve.
 *
 * The target to driver frequency mapping is cached in the policy.
 *
 * Return: Lowest driver-supported frequency greater than or equal to the
 * given target_freq, subject to policy (min/max) and driver limitations.
 */
unsigned int cpufreq_driver_resolve_freq(struct cpufreq_policy *policy,
					 unsigned int target_freq)
{
	target_freq = clamp_val(target_freq, policy->min, policy->max);
	policy->cached_target_freq = target_freq;
516 517 518 519 520 521 522 523 524 525

	if (cpufreq_driver->target_index) {
		int idx;

		idx = cpufreq_frequency_table_target(policy, target_freq,
						     CPUFREQ_RELATION_L);
		policy->cached_resolved_idx = idx;
		return policy->freq_table[idx].frequency;
	}

526 527
	if (cpufreq_driver->resolve_freq)
		return cpufreq_driver->resolve_freq(policy, target_freq);
528 529

	return target_freq;
530
}
531
EXPORT_SYMBOL_GPL(cpufreq_driver_resolve_freq);
532

533 534 535 536 537 538 539 540
unsigned int cpufreq_policy_transition_delay_us(struct cpufreq_policy *policy)
{
	unsigned int latency;

	if (policy->transition_delay_us)
		return policy->transition_delay_us;

	latency = policy->cpuinfo.transition_latency / NSEC_PER_USEC;
541 542 543 544 545 546 547 548 549 550 551 552 553
	if (latency) {
		/*
		 * For platforms that can change the frequency very fast (< 10
		 * us), the above formula gives a decent transition delay. But
		 * for platforms where transition_latency is in milliseconds, it
		 * ends up giving unrealistic values.
		 *
		 * Cap the default transition delay to 10 ms, which seems to be
		 * a reasonable amount of time after which we should reevaluate
		 * the frequency.
		 */
		return min(latency * LATENCY_MULTIPLIER, (unsigned int)10000);
	}
554 555 556 557 558

	return LATENCY_MULTIPLIER;
}
EXPORT_SYMBOL_GPL(cpufreq_policy_transition_delay_us);

Linus Torvalds's avatar
Linus Torvalds committed
559 560 561
/*********************************************************************
 *                          SYSFS INTERFACE                          *
 *********************************************************************/
562
static ssize_t show_boost(struct kobject *kobj,
563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
				 struct attribute *attr, char *buf)
{
	return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
}

static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
				  const char *buf, size_t count)
{
	int ret, enable;

	ret = sscanf(buf, "%d", &enable);
	if (ret != 1 || enable < 0 || enable > 1)
		return -EINVAL;

	if (cpufreq_boost_trigger_state(enable)) {
578 579
		pr_err("%s: Cannot %s BOOST!\n",
		       __func__, enable ? "enable" : "disable");
580 581 582
		return -EINVAL;
	}

583 584
	pr_debug("%s: cpufreq BOOST %s\n",
		 __func__, enable ? "enabled" : "disabled");
585 586 587 588

	return count;
}
define_one_global_rw(boost);
Linus Torvalds's avatar
Linus Torvalds committed
589

590
static struct cpufreq_governor *find_governor(const char *str_governor)
591 592 593
{
	struct cpufreq_governor *t;

594
	for_each_governor(t)
595
		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
596 597 598 599 600
			return t;

	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
601 602 603
/**
 * cpufreq_parse_governor - parse a governor string
 */
Dave Jones's avatar
Dave Jones committed
604
static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds's avatar
Linus Torvalds committed
605 606
				struct cpufreq_governor **governor)
{
607
	int err = -EINVAL;
608 609

	if (cpufreq_driver->setpolicy) {
610
		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds's avatar
Linus Torvalds committed
611
			*policy = CPUFREQ_POLICY_PERFORMANCE;
612
			err = 0;
613
		} else if (!strncasecmp(str_governor, "powersave",
614
						CPUFREQ_NAME_LEN)) {
Linus Torvalds's avatar
Linus Torvalds committed
615
			*policy = CPUFREQ_POLICY_POWERSAVE;
616
			err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
617
		}
618
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
619
		struct cpufreq_governor *t;
620

621
		mutex_lock(&cpufreq_governor_mutex);
622

623
		t = find_governor(str_governor);
624

625
		if (t == NULL) {
626
			int ret;
627

628 629 630
			mutex_unlock(&cpufreq_governor_mutex);
			ret = request_module("cpufreq_%s", str_governor);
			mutex_lock(&cpufreq_governor_mutex);
631

632
			if (ret == 0)
633
				t = find_governor(str_governor);
634 635
		}

636 637 638
		if (t != NULL) {
			*governor = t;
			err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
639
		}
640

641
		mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
642
	}
643
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
644 645 646
}

/**
647 648
 * cpufreq_per_cpu_attr_read() / show_##file_name() -
 * print out cpufreq information
Linus Torvalds's avatar
Linus Torvalds committed
649 650 651 652 653
 *
 * Write out information from cpufreq_driver->policy[cpu]; object must be
 * "unsigned int".
 */

654 655
#define show_one(file_name, object)			\
static ssize_t show_##file_name				\
Dave Jones's avatar
Dave Jones committed
656
(struct cpufreq_policy *policy, char *buf)		\
657
{							\
658
	return sprintf(buf, "%u\n", policy->object);	\
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662
}

show_one(cpuinfo_min_freq, cpuinfo.min_freq);
show_one(cpuinfo_max_freq, cpuinfo.max_freq);
663
show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds's avatar
Linus Torvalds committed
664 665
show_one(scaling_min_freq, min);
show_one(scaling_max_freq, max);
666

667 668 669 670 671
__weak unsigned int arch_freq_get_on_cpu(int cpu)
{
	return 0;
}

672
static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
673 674
{
	ssize_t ret;
675
	unsigned int freq;
676

677 678 679 680 681
	freq = arch_freq_get_on_cpu(policy->cpu);
	if (freq)
		ret = sprintf(buf, "%u\n", freq);
	else if (cpufreq_driver && cpufreq_driver->setpolicy &&
			cpufreq_driver->get)
682 683 684 685 686
		ret = sprintf(buf, "%u\n", cpufreq_driver->get(policy->cpu));
	else
		ret = sprintf(buf, "%u\n", policy->cur);
	return ret;
}
Linus Torvalds's avatar
Linus Torvalds committed
687

688
static int cpufreq_set_policy(struct cpufreq_policy *policy,
689
				struct cpufreq_policy *new_policy);
690

Linus Torvalds's avatar
Linus Torvalds committed
691 692 693 694 695
/**
 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
 */
#define store_one(file_name, object)			\
static ssize_t store_##file_name					\
Dave Jones's avatar
Dave Jones committed
696
(struct cpufreq_policy *policy, const char *buf, size_t count)		\
Linus Torvalds's avatar
Linus Torvalds committed
697
{									\
698
	int ret, temp;							\
Linus Torvalds's avatar
Linus Torvalds committed
699 700
	struct cpufreq_policy new_policy;				\
									\
701
	memcpy(&new_policy, policy, sizeof(*policy));			\
Linus Torvalds's avatar
Linus Torvalds committed
702
									\
703
	ret = sscanf(buf, "%u", &new_policy.object);			\
Linus Torvalds's avatar
Linus Torvalds committed
704 705 706
	if (ret != 1)							\
		return -EINVAL;						\
									\
707
	temp = new_policy.object;					\
708
	ret = cpufreq_set_policy(policy, &new_policy);		\
709 710
	if (!ret)							\
		policy->user_policy.object = temp;			\
Linus Torvalds's avatar
Linus Torvalds committed
711 712 713 714
									\
	return ret ? ret : count;					\
}

715 716
store_one(scaling_min_freq, min);
store_one(scaling_max_freq, max);
Linus Torvalds's avatar
Linus Torvalds committed
717 718 719 720

/**
 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
 */
Dave Jones's avatar
Dave Jones committed
721 722
static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
					char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
723
{
724
	unsigned int cur_freq = __cpufreq_get(policy);
725 726 727 728 729

	if (cur_freq)
		return sprintf(buf, "%u\n", cur_freq);

	return sprintf(buf, "<unknown>\n");
Linus Torvalds's avatar
Linus Torvalds committed
730 731 732 733 734
}

/**
 * show_scaling_governor - show the current policy for the specified CPU
 */
Dave Jones's avatar
Dave Jones committed
735
static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
736
{
737
	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740 741
		return sprintf(buf, "powersave\n");
	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
		return sprintf(buf, "performance\n");
	else if (policy->governor)
742
		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
743
				policy->governor->name);
Linus Torvalds's avatar
Linus Torvalds committed
744 745 746 747 748 749
	return -EINVAL;
}

/**
 * store_scaling_governor - store policy for the specified CPU
 */
Dave Jones's avatar
Dave Jones committed
750 751
static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
					const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
752
{
753
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756
	char	str_governor[16];
	struct cpufreq_policy new_policy;

757
	memcpy(&new_policy, policy, sizeof(*policy));
Linus Torvalds's avatar
Linus Torvalds committed
758

759
	ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds's avatar
Linus Torvalds committed
760 761 762
	if (ret != 1)
		return -EINVAL;

763 764
	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
						&new_policy.governor))
Linus Torvalds's avatar
Linus Torvalds committed
765 766
		return -EINVAL;

767
	ret = cpufreq_set_policy(policy, &new_policy);
768
	return ret ? ret : count;
Linus Torvalds's avatar
Linus Torvalds committed
769 770 771 772 773
}

/**
 * show_scaling_driver - show the cpufreq driver currently loaded
 */
Dave Jones's avatar
Dave Jones committed
774
static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
775
{
776
	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds's avatar
Linus Torvalds committed
777 778 779 780 781
}

/**
 * show_scaling_available_governors - show the available CPUfreq governors
 */
Dave Jones's avatar
Dave Jones committed
782 783
static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
						char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
784 785 786 787
{
	ssize_t i = 0;
	struct cpufreq_governor *t;

788
	if (!has_target()) {
Linus Torvalds's avatar
Linus Torvalds committed
789 790 791 792
		i += sprintf(buf, "performance powersave");
		goto out;
	}

793
	for_each_governor(t) {
794 795
		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
		    - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds's avatar
Linus Torvalds committed
796
			goto out;
797
		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds's avatar
Linus Torvalds committed
798
	}
799
out:
Linus Torvalds's avatar
Linus Torvalds committed
800 801 802
	i += sprintf(&buf[i], "\n");
	return i;
}
803

804
ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
805 806 807 808
{
	ssize_t i = 0;
	unsigned int cpu;

809
	for_each_cpu(cpu, mask) {
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813
		if (i)
			i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
		i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
		if (i >= (PAGE_SIZE - 5))
814
			break;
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818
	}
	i += sprintf(&buf[i], "\n");
	return i;
}
819
EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds's avatar
Linus Torvalds committed
820

821 822 823 824 825 826
/**
 * show_related_cpus - show the CPUs affected by each transition even if
 * hw coordination is in use
 */
static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
{
827
	return cpufreq_show_cpus(policy->related_cpus, buf);
828 829 830 831 832 833 834
}

/**
 * show_affected_cpus - show the CPUs affected by each transition
 */
static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
{
835
	return cpufreq_show_cpus(policy->cpus, buf);
836 837
}

838
static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones's avatar
Dave Jones committed
839
					const char *buf, size_t count)
840 841 842 843
{
	unsigned int freq = 0;
	unsigned int ret;

844
	if (!policy->governor || !policy->governor->store_setspeed)
845 846 847 848 849 850 851 852 853 854 855 856 857
		return -EINVAL;

	ret = sscanf(buf, "%u", &freq);
	if (ret != 1)
		return -EINVAL;

	policy->governor->store_setspeed(policy, freq);

	return count;
}

static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
{
858
	if (!policy->governor || !policy->governor->show_setspeed)
859 860 861 862
		return sprintf(buf, "<unsupported>\n");

	return policy->governor->show_setspeed(policy, buf);
}
Linus Torvalds's avatar
Linus Torvalds committed
863

864
/**
865
 * show_bios_limit - show the current cpufreq HW/BIOS limitation
866 867 868 869 870
 */
static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
{
	unsigned int limit;
	int ret;
871 872
	if (cpufreq_driver->bios_limit) {
		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
873 874 875 876 877 878
		if (!ret)
			return sprintf(buf, "%u\n", limit);
	}
	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
}

879 880 881 882 883 884 885 886 887 888 889 890 891 892
cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
cpufreq_freq_attr_ro(cpuinfo_min_freq);
cpufreq_freq_attr_ro(cpuinfo_max_freq);
cpufreq_freq_attr_ro(cpuinfo_transition_latency);
cpufreq_freq_attr_ro(scaling_available_governors);
cpufreq_freq_attr_ro(scaling_driver);
cpufreq_freq_attr_ro(scaling_cur_freq);
cpufreq_freq_attr_ro(bios_limit);
cpufreq_freq_attr_ro(related_cpus);
cpufreq_freq_attr_ro(affected_cpus);
cpufreq_freq_attr_rw(scaling_min_freq);
cpufreq_freq_attr_rw(scaling_max_freq);
cpufreq_freq_attr_rw(scaling_governor);
cpufreq_freq_attr_rw(scaling_setspeed);
Linus Torvalds's avatar
Linus Torvalds committed
893

Dave Jones's avatar
Dave Jones committed
894
static struct attribute *default_attrs[] = {
Linus Torvalds's avatar
Linus Torvalds committed
895 896
	&cpuinfo_min_freq.attr,
	&cpuinfo_max_freq.attr,
897
	&cpuinfo_transition_latency.attr,
Linus Torvalds's avatar
Linus Torvalds committed
898 899 900
	&scaling_min_freq.attr,
	&scaling_max_freq.attr,
	&affected_cpus.attr,
901
	&related_cpus.attr,
Linus Torvalds's avatar
Linus Torvalds committed
902 903 904
	&scaling_governor.attr,
	&scaling_driver.attr,
	&scaling_available_governors.attr,
905
	&scaling_setspeed.attr,
Linus Torvalds's avatar
Linus Torvalds committed
906 907 908
	NULL
};

909 910
#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
#define to_attr(a) container_of(a, struct freq_attr, attr)
Linus Torvalds's avatar
Linus Torvalds committed
911

912
static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
913
{
Dave Jones's avatar
Dave Jones committed
914 915
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
916
	ssize_t ret;
917

918
	down_read(&policy->rwsem);
919
	ret = fattr->show(policy, buf);
920
	up_read(&policy->rwsem);
921

Linus Torvalds's avatar
Linus Torvalds committed
922 923 924
	return ret;
}

Dave Jones's avatar
Dave Jones committed
925 926
static ssize_t store(struct kobject *kobj, struct attribute *attr,
		     const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
927
{
Dave Jones's avatar
Dave Jones committed
928 929
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
930
	ssize_t ret = -EINVAL;
931

932
	cpus_read_lock();
933

934 935
	if (cpu_online(policy->cpu)) {
		down_write(&policy->rwsem);
936
		ret = fattr->store(policy, buf, count);
937 938
		up_write(&policy->rwsem);
	}
939

940
	cpus_read_unlock();
941

Linus Torvalds's avatar
Linus Torvalds committed
942 943 944
	return ret;
}

Dave Jones's avatar
Dave Jones committed
945
static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds's avatar
Linus Torvalds committed
946
{
Dave Jones's avatar
Dave Jones committed
947
	struct cpufreq_policy *policy = to_policy(kobj);
948
	pr_debug("last reference is dropped\n");
Linus Torvalds's avatar
Linus Torvalds committed
949 950 951
	complete(&policy->kobj_unregister);
}

952
static const struct sysfs_ops sysfs_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
953 954 955 956 957 958 959 960 961 962
	.show	= show,
	.store	= store,
};

static struct kobj_type ktype_cpufreq = {
	.sysfs_ops	= &sysfs_ops,
	.default_attrs	= default_attrs,
	.release	= cpufreq_sysfs_release,
};

963
static void add_cpu_dev_symlink(struct cpufreq_policy *policy, unsigned int cpu)
964
{
965 966 967 968 969 970 971 972
	struct device *dev = get_cpu_device(cpu);

	if (!dev)
		return;

	if (cpumask_test_and_set_cpu(cpu, policy->real_cpus))
		return;

973
	dev_dbg(dev, "%s: Adding symlink\n", __func__);
974 975
	if (sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq"))
		dev_err(dev, "cpufreq symlink creation failed\n");
976 977
}

978 979
static void remove_cpu_dev_symlink(struct cpufreq_policy *policy,
				   struct device *dev)
980
{
981 982
	dev_dbg(dev, "%s: Removing symlink\n", __func__);
	sysfs_remove_link(&dev->kobj, "cpufreq");
983 984
}

985
static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
986 987 988 989 990
{
	struct freq_attr **drv_attr;
	int ret = 0;

	/* set up files for this cpu device */
991
	drv_attr = cpufreq_driver->attr;
992
	while (drv_attr && *drv_attr) {
993 994
		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
		if (ret)
995
			return ret;
996 997
		drv_attr++;
	}
998
	if (cpufreq_driver->get) {
999 1000
		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
		if (ret)
1001
			return ret;
1002
	}
1003 1004 1005

	ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
	if (ret)
1006
		return ret;
1007

1008
	if (cpufreq_driver->bios_limit) {
1009 1010
		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
		if (ret)
1011
			return ret;
1012
	}
1013

1014
	return 0;
1015 1016
}

1017 1018 1019 1020 1021
__weak struct cpufreq_governor *cpufreq_default_governor(void)
{
	return NULL;
}

1022
static int cpufreq_init_policy(struct cpufreq_policy *policy)
1023
{
1024
	struct cpufreq_governor *gov = NULL;
1025 1026
	struct cpufreq_policy new_policy;

1027
	memcpy(&new_policy, policy, sizeof(*policy));
1028

1029
	/* Update governor of new_policy to the governor used before hotplug */
1030
	gov = find_governor(policy->last_governor);
1031
	if (gov) {
1032 1033
		pr_debug("Restoring governor %s for cpu %d\n",
				policy->governor->name, policy->cpu);
1034 1035 1036 1037 1038
	} else {
		gov = cpufreq_default_governor();
		if (!gov)
			return -ENODATA;
	}
1039 1040 1041

	new_policy.governor = gov;

1042 1043 1044 1045 1046 1047 1048 1049
	/* Use the default policy if there is no last_policy. */
	if (cpufreq_driver->setpolicy) {
		if (policy->last_policy)
			new_policy.policy = policy->last_policy;
		else
			cpufreq_parse_governor(gov->name, &new_policy.policy,
					       NULL);
	}
1050
	/* set default policy */
1051
	return cpufreq_set_policy(policy, &new_policy);
1052 1053
}

1054
static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
1055
{
1056
	int ret = 0;
1057

1058 1059 1060 1061
	/* Has this CPU been taken care of already? */
	if (cpumask_test_cpu(cpu, policy->cpus))
		return 0;

1062
	down_write(&policy->rwsem);
1063 1064
	if (has_target())
		cpufreq_stop_governor(policy);
1065 1066

	cpumask_set_cpu(cpu, policy->cpus);
1067

1068
	if (has_target()) {
1069
		ret = cpufreq_start_governor(policy);
1070
		if (ret)
1071
			pr_err("%s: Failed to start governor\n", __func__);
1072
	}
1073 1074
	up_write(&policy->rwsem);
	return ret;
1075
}
Linus Torvalds's avatar
Linus Torvalds committed
1076

1077 1078 1079 1080 1081 1082 1083
static void handle_update(struct work_struct *work)
{
	struct cpufreq_policy *policy =
		container_of(work, struct cpufreq_policy, update);
	unsigned int cpu = policy->cpu;
	pr_debug("handle_update for cpu %u called\n", cpu);
	cpufreq_update_policy(cpu);
1084
}
Linus Torvalds's avatar
Linus Torvalds committed
1085

1086
static struct cpufreq_policy *cpufreq_policy_alloc(unsigned int cpu)
1087 1088
{
	struct cpufreq_policy *policy;
1089
	int ret;
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100

	policy = kzalloc(sizeof(*policy), GFP_KERNEL);
	if (!policy)
		return NULL;

	if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
		goto err_free_policy;

	if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
		goto err_free_cpumask;

1101 1102 1103
	if (!zalloc_cpumask_var(&policy->real_cpus, GFP_KERNEL))
		goto err_free_rcpumask;

1104 1105 1106 1107 1108 1109 1110
	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
				   cpufreq_global_kobject, "policy%u", cpu);
	if (ret) {
		pr_err("%s: failed to init policy->kobj: %d\n", __func__, ret);
		goto err_free_real_cpus;
	}

1111
	INIT_LIST_HEAD(&policy->policy_list);
1112
	init_rwsem(&policy->rwsem);
1113 1114
	spin_lock_init(&policy->transition_lock);
	init_waitqueue_head(&policy->transition_wait);
1115 1116
	init_completion(&policy->kobj_unregister);
	INIT_WORK(&policy->update, handle_update);
1117

1118
	policy->cpu = cpu;
1119 1120
	return policy;

1121 1122
err_free_real_cpus:
	free_cpumask_var(policy->real_cpus);
1123 1124
err_free_rcpumask:
	free_cpumask_var(policy->related_cpus);
1125 1126 1127 1128 1129 1130 1131 1132
err_free_cpumask:
	free_cpumask_var(policy->cpus);
err_free_policy:
	kfree(policy);

	return NULL;
}

1133
static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
1134 1135 1136 1137
{
	struct kobject *kobj;
	struct completion *cmp;

1138
	down_write(&policy->rwsem);
1139
	cpufreq_stats_free_table(policy);
1140 1141
	kobj = &policy->kobj;
	cmp = &policy->kobj_unregister;
1142
	up_write(&policy->rwsem);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
	kobject_put(kobj);

	/*
	 * We need to make sure that the underlying kobj is
	 * actually not referenced anymore by anybody before we
	 * proceed with unloading.
	 */
	pr_debug("waiting for dropping of refcount\n");
	wait_for_completion(cmp);
	pr_debug("wait complete\n");
}

1155
static void cpufreq_policy_free(struct cpufreq_policy *policy)
1156
{
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
	unsigned long flags;
	int cpu;

	/* Remove policy from list */
	write_lock_irqsave(&cpufreq_driver_lock, flags);
	list_del(&policy->policy_list);

	for_each_cpu(cpu, policy->related_cpus)
		per_cpu(cpufreq_cpu_data, cpu) = NULL;
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);

1168
	cpufreq_policy_put_kobj(policy);
1169
	free_cpumask_var(policy->real_cpus);
1170 1171 1172 1173 1174
	free_cpumask_var(policy->related_cpus);
	free_cpumask_var(policy->cpus);
	kfree(policy);
}

1175
static int cpufreq_online(unsigned int cpu)
Linus Torvalds's avatar
Linus Torvalds committed
1176
{
1177
	struct cpufreq_policy *policy;
1178
	bool new_policy;
Linus Torvalds's avatar
Linus Torvalds committed
1179
	unsigned long flags;
1180 1181
	unsigned int j;
	int ret;
1182

1183
	pr_debug("%s: bringing CPU%u online\n", __func__, cpu);
1184

1185
	/* Check if this CPU already has a policy to manage it */
1186
	policy = per_cpu(cpufreq_cpu_data, cpu);
1187
	if (policy) {
1188
		WARN_ON(!cpumask_test_cpu(cpu, policy->related_cpus));
1189
		if (!policy_is_inactive(policy))
1190
			return cpufreq_add_policy_cpu(policy, cpu);
Linus Torvalds's avatar
Linus Torvalds committed
1191

1192
		/* This is the only online CPU for the policy.  Start over. */
1193
		new_policy = false;
1194 1195 1196 1197 1198
		down_write(&policy->rwsem);
		policy->cpu = cpu;
		policy->governor = NULL;
		up_write(&policy->rwsem);
	} else {
1199
		new_policy = true;
1200
		policy = cpufreq_policy_alloc(cpu);
1201
		if (!policy)
1202
			return -ENOMEM;
1203
	}
1204

1205
	cpumask_copy(policy->cpus, cpumask_of(cpu));
Linus Torvalds's avatar
Linus Torvalds committed
1206 1207 1208 1209

	/* call driver. From then on the cpufreq must be able
	 * to accept all calls to ->verify and ->setpolicy for this CPU
	 */
1210
	ret = cpufreq_driver->init(policy);
Linus Torvalds's avatar
Linus Torvalds committed
1211
	if (ret) {
1212
		pr_debug("initialization failed\n");
1213
		goto out_free_policy;
Linus Torvalds's avatar
Linus Torvalds committed
1214
	}
1215

1216 1217
	down_write(&policy->rwsem);

1218
	if (new_policy) {
1219
		/* related_cpus should at least include policy->cpus. */
1220
		cpumask_copy(policy->related_cpus, policy->cpus);
1221
	}
1222

1223 1224 1225 1226 1227 1228
	/*
	 * affected cpus must always be the one, which are online. We aren't
	 * managing offline cpus here.
	 */
	cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);

1229
	if (new_policy) {
1230 1231
		policy->user_policy.min = policy->min;
		policy->user_policy.max = policy->max;
1232

1233
		for_each_cpu(j, policy->related_cpus) {
1234
			per_cpu(cpufreq_cpu_data, j) = policy;
1235 1236
			add_cpu_dev_symlink(policy, j);
		}
1237 1238 1239
	} else {
		policy->min = policy->user_policy.min;
		policy->max = policy->user_policy.max;
1240
	}
1241

1242
	if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1243 1244 1245
		policy->cur = cpufreq_driver->get(policy->cpu);
		if (!policy->cur) {
			pr_err("%s: ->get() failed\n", __func__);
1246
			goto out_exit_policy;
1247 1248 1249
		}
	}

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
	/*
	 * Sometimes boot loaders set CPU frequency to a value outside of
	 * frequency table present with cpufreq core. In such cases CPU might be
	 * unstable if it has to run on that frequency for long duration of time
	 * and so its better to set it to a frequency which is specified in
	 * freq-table. This also makes cpufreq stats inconsistent as
	 * cpufreq-stats would fail to register because current frequency of CPU
	 * isn't found in freq-table.
	 *
	 * Because we don't want this change to effect boot process badly, we go
	 * for the next freq which is >= policy->cur ('cur' must be set by now,
	 * otherwise we will end up setting freq to lowest of the table as 'cur'
	 * is initialized to zero).
	 *
	 * We are passing target-freq as "policy->cur - 1" otherwise
	 * __cpufreq_driver_target() would simply fail, as policy->cur will be
	 * equal to target-freq.
	 */
	if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
	    && has_target()) {
		/* Are we running at unknown frequency ? */
		ret = cpufreq_frequency_table_get_index(policy, policy->cur);
		if (ret == -EINVAL) {
			/* Warn user and fix it */
			pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
				__func__, policy->cpu, policy->cur);
			ret = __cpufreq_driver_target(policy, policy->cur - 1,
				CPUFREQ_RELATION_L);

			/*
			 * Reaching here after boot in a few seconds may not
			 * mean that system will remain stable at "unknown"
			 * frequency for longer duration. Hence, a BUG_ON().
			 */
			BUG_ON(ret);
			pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
				__func__, policy->cpu, policy->cur);
		}
	}

1290
	if (new_policy) {
1291
		ret = cpufreq_add_dev_interface(policy);