cpufreq.c 57.5 KB
Newer Older
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
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
 *
L
Linus Torvalds 已提交
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.
 */

V
Viresh Kumar 已提交
18 19
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

20
#include <linux/cpu.h>
L
Linus Torvalds 已提交
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/syscore_ops.h>
30
#include <linux/tick.h>
31 32
#include <trace/events/power.h>

L
Linus Torvalds 已提交
33
/**
D
Dave Jones 已提交
34
 * The "cpufreq driver" - the arch- or hardware-dependent low
L
Linus Torvalds 已提交
35 36 37
 * level driver of CPUFreq support, and its spinlock. This lock
 * also protects the cpufreq_cpu_data array.
 */
38
static struct cpufreq_driver *cpufreq_driver;
39
static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
40
static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41
static DEFINE_RWLOCK(cpufreq_driver_lock);
42
DEFINE_MUTEX(cpufreq_governor_lock);
43
static LIST_HEAD(cpufreq_policy_list);
44

45 46
#ifdef CONFIG_HOTPLUG_CPU
/* This one keeps track of the previously set governor of a removed CPU */
47
static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
48
#endif
L
Linus Torvalds 已提交
49

50 51 52 53 54
static inline bool has_target(void)
{
	return cpufreq_driver->target_index || cpufreq_driver->target;
}

55 56 57 58 59 60
/*
 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
 * sections
 */
static DECLARE_RWSEM(cpufreq_rwsem);

L
Linus Torvalds 已提交
61
/* internal prototypes */
62 63
static int __cpufreq_governor(struct cpufreq_policy *policy,
		unsigned int event);
64
static unsigned int __cpufreq_get(unsigned int cpu);
65
static void handle_update(struct work_struct *work);
L
Linus Torvalds 已提交
66 67

/**
68 69
 * Two notifier lists: the "policy" list is involved in the
 * validation process for a new CPU frequency policy; the
L
Linus Torvalds 已提交
70 71 72 73
 * "transition" list for kernel code that needs to handle
 * changes to devices when the CPU clock speed changes.
 * The mutex locks both lists.
 */
74
static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
75
static struct srcu_notifier_head cpufreq_transition_notifier_list;
L
Linus Torvalds 已提交
76

77
static bool init_cpufreq_transition_notifier_list_called;
78 79 80
static int __init init_cpufreq_transition_notifier_list(void)
{
	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
81
	init_cpufreq_transition_notifier_list_called = true;
82 83
	return 0;
}
84
pure_initcall(init_cpufreq_transition_notifier_list);
L
Linus Torvalds 已提交
85

86
static int off __read_mostly;
87
static int cpufreq_disabled(void)
88 89 90 91 92 93 94
{
	return off;
}
void disable_cpufreq(void)
{
	off = 1;
}
L
Linus Torvalds 已提交
95
static LIST_HEAD(cpufreq_governor_list);
96
static DEFINE_MUTEX(cpufreq_governor_mutex);
L
Linus Torvalds 已提交
97

98 99
bool have_governor_per_policy(void)
{
100
	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
101
}
102
EXPORT_SYMBOL_GPL(have_governor_per_policy);
103

104 105 106 107 108 109 110 111 112
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);

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
{
	u64 idle_time;
	u64 cur_wall_time;
	u64 busy_time;

	cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());

	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)
		*wall = cputime_to_usecs(cur_wall_time);

	return cputime_to_usecs(idle_time);
}

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);

148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/*
 * 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;

	/*
	 * The driver only supports the SMP configuartion where all processors
	 * share the clock and voltage and clock.
	 */
	cpumask_setall(policy->cpus);

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_generic_init);

179
struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
L
Linus Torvalds 已提交
180
{
181
	struct cpufreq_policy *policy = NULL;
L
Linus Torvalds 已提交
182 183
	unsigned long flags;

184 185 186 187 188
	if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
		return NULL;

	if (!down_read_trylock(&cpufreq_rwsem))
		return NULL;
L
Linus Torvalds 已提交
189 190

	/* get the cpufreq driver */
191
	read_lock_irqsave(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
192

193 194 195 196 197 198
	if (cpufreq_driver) {
		/* get the CPU */
		policy = per_cpu(cpufreq_cpu_data, cpu);
		if (policy)
			kobject_get(&policy->kobj);
	}
L
Linus Torvalds 已提交
199

200
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
201

202
	if (!policy)
203
		up_read(&cpufreq_rwsem);
L
Linus Torvalds 已提交
204

205
	return policy;
206
}
L
Linus Torvalds 已提交
207 208
EXPORT_SYMBOL_GPL(cpufreq_cpu_get);

209
void cpufreq_cpu_put(struct cpufreq_policy *policy)
L
Linus Torvalds 已提交
210
{
211 212 213
	if (cpufreq_disabled())
		return;

214 215
	kobject_put(&policy->kobj);
	up_read(&cpufreq_rwsem);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227
}
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
228
 * systems as each CPU might be scaled differently. So, use the arch
L
Linus Torvalds 已提交
229 230 231 232
 * per-CPU loops_per_jiffy value wherever possible.
 */
#ifndef CONFIG_SMP
static unsigned long l_p_j_ref;
233
static unsigned int l_p_j_ref_freq;
L
Linus Torvalds 已提交
234

235
static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
L
Linus Torvalds 已提交
236 237 238 239 240 241 242
{
	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;
243
		pr_debug("saving %lu as reference value for loops_per_jiffy; "
244
			"freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
L
Linus Torvalds 已提交
245
	}
246
	if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
247
	    (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
248 249
		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
								ci->new);
250
		pr_debug("scaling loops_per_jiffy to %lu "
251
			"for frequency %u kHz\n", loops_per_jiffy, ci->new);
L
Linus Torvalds 已提交
252 253 254
	}
}
#else
255 256 257 258
static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
{
	return;
}
L
Linus Torvalds 已提交
259 260
#endif

261
static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
262
		struct cpufreq_freqs *freqs, unsigned int state)
L
Linus Torvalds 已提交
263 264 265
{
	BUG_ON(irqs_disabled());

266 267 268
	if (cpufreq_disabled())
		return;

269
	freqs->flags = cpufreq_driver->flags;
270
	pr_debug("notification %u of frequency transition to %u kHz\n",
271
		state, freqs->new);
L
Linus Torvalds 已提交
272 273

	switch (state) {
274

L
Linus Torvalds 已提交
275
	case CPUFREQ_PRECHANGE:
276
		/* detect if the driver reported a value as "old frequency"
277 278
		 * which is not equal to what the cpufreq core thinks is
		 * "old frequency".
L
Linus Torvalds 已提交
279
		 */
280
		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
281 282
			if ((policy) && (policy->cpu == freqs->cpu) &&
			    (policy->cur) && (policy->cur != freqs->old)) {
283
				pr_debug("Warning: CPU frequency is"
284 285 286
					" %u, cpufreq assumed %u kHz.\n",
					freqs->old, policy->cur);
				freqs->old = policy->cur;
L
Linus Torvalds 已提交
287 288
			}
		}
289
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
290
				CPUFREQ_PRECHANGE, freqs);
L
Linus Torvalds 已提交
291 292
		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
		break;
293

L
Linus Torvalds 已提交
294 295
	case CPUFREQ_POSTCHANGE:
		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
296
		pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
297
			(unsigned long)freqs->cpu);
298
		trace_cpu_frequency(freqs->new, freqs->cpu);
299
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
300
				CPUFREQ_POSTCHANGE, freqs);
301 302
		if (likely(policy) && likely(policy->cpu == freqs->cpu))
			policy->cur = freqs->new;
L
Linus Torvalds 已提交
303 304 305
		break;
	}
}
306

307 308 309 310 311 312 313 314 315 316 317 318 319 320
/**
 * 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.
 */
void cpufreq_notify_transition(struct cpufreq_policy *policy,
		struct cpufreq_freqs *freqs, unsigned int state)
{
	for_each_cpu(freqs->cpu, policy->cpus)
		__cpufreq_notify_transition(policy, freqs, state);
}
L
Linus Torvalds 已提交
321 322
EXPORT_SYMBOL_GPL(cpufreq_notify_transition);

323 324 325 326 327 328 329 330 331 332 333 334 335 336
/* Do post notifications when there are chances that transition has failed */
void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
		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);
}
EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);

L
Linus Torvalds 已提交
337 338 339 340 341

/*********************************************************************
 *                          SYSFS INTERFACE                          *
 *********************************************************************/

342 343 344 345 346
static struct cpufreq_governor *__find_governor(const char *str_governor)
{
	struct cpufreq_governor *t;

	list_for_each_entry(t, &cpufreq_governor_list, governor_list)
347
		if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
348 349 350 351 352
			return t;

	return NULL;
}

L
Linus Torvalds 已提交
353 354 355
/**
 * cpufreq_parse_governor - parse a governor string
 */
D
Dave Jones 已提交
356
static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
L
Linus Torvalds 已提交
357 358
				struct cpufreq_governor **governor)
{
359
	int err = -EINVAL;
360 361

	if (!cpufreq_driver)
362 363
		goto out;

364
	if (cpufreq_driver->setpolicy) {
L
Linus Torvalds 已提交
365 366
		if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
			*policy = CPUFREQ_POLICY_PERFORMANCE;
367
			err = 0;
368 369
		} else if (!strnicmp(str_governor, "powersave",
						CPUFREQ_NAME_LEN)) {
L
Linus Torvalds 已提交
370
			*policy = CPUFREQ_POLICY_POWERSAVE;
371
			err = 0;
L
Linus Torvalds 已提交
372
		}
373
	} else if (has_target()) {
L
Linus Torvalds 已提交
374
		struct cpufreq_governor *t;
375

376
		mutex_lock(&cpufreq_governor_mutex);
377 378 379

		t = __find_governor(str_governor);

380
		if (t == NULL) {
381
			int ret;
382

383 384 385
			mutex_unlock(&cpufreq_governor_mutex);
			ret = request_module("cpufreq_%s", str_governor);
			mutex_lock(&cpufreq_governor_mutex);
386

387 388
			if (ret == 0)
				t = __find_governor(str_governor);
389 390
		}

391 392 393
		if (t != NULL) {
			*governor = t;
			err = 0;
L
Linus Torvalds 已提交
394
		}
395

396
		mutex_unlock(&cpufreq_governor_mutex);
L
Linus Torvalds 已提交
397
	}
398
out:
399
	return err;
L
Linus Torvalds 已提交
400 401 402
}

/**
403 404
 * cpufreq_per_cpu_attr_read() / show_##file_name() -
 * print out cpufreq information
L
Linus Torvalds 已提交
405 406 407 408 409
 *
 * Write out information from cpufreq_driver->policy[cpu]; object must be
 * "unsigned int".
 */

410 411
#define show_one(file_name, object)			\
static ssize_t show_##file_name				\
D
Dave Jones 已提交
412
(struct cpufreq_policy *policy, char *buf)		\
413
{							\
414
	return sprintf(buf, "%u\n", policy->object);	\
L
Linus Torvalds 已提交
415 416 417 418
}

show_one(cpuinfo_min_freq, cpuinfo.min_freq);
show_one(cpuinfo_max_freq, cpuinfo.max_freq);
419
show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
L
Linus Torvalds 已提交
420 421 422 423
show_one(scaling_min_freq, min);
show_one(scaling_max_freq, max);
show_one(scaling_cur_freq, cur);

424
static int cpufreq_set_policy(struct cpufreq_policy *policy,
425
				struct cpufreq_policy *new_policy);
426

L
Linus Torvalds 已提交
427 428 429 430 431
/**
 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
 */
#define store_one(file_name, object)			\
static ssize_t store_##file_name					\
D
Dave Jones 已提交
432
(struct cpufreq_policy *policy, const char *buf, size_t count)		\
L
Linus Torvalds 已提交
433
{									\
434
	int ret;							\
L
Linus Torvalds 已提交
435 436 437 438 439 440
	struct cpufreq_policy new_policy;				\
									\
	ret = cpufreq_get_policy(&new_policy, policy->cpu);		\
	if (ret)							\
		return -EINVAL;						\
									\
441
	ret = sscanf(buf, "%u", &new_policy.object);			\
L
Linus Torvalds 已提交
442 443 444
	if (ret != 1)							\
		return -EINVAL;						\
									\
445
	ret = cpufreq_set_policy(policy, &new_policy);		\
446
	policy->user_policy.object = policy->object;			\
L
Linus Torvalds 已提交
447 448 449 450
									\
	return ret ? ret : count;					\
}

451 452
store_one(scaling_min_freq, min);
store_one(scaling_max_freq, max);
L
Linus Torvalds 已提交
453 454 455 456

/**
 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
 */
D
Dave Jones 已提交
457 458
static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
					char *buf)
L
Linus Torvalds 已提交
459
{
460
	unsigned int cur_freq = __cpufreq_get(policy->cpu);
L
Linus Torvalds 已提交
461 462 463 464 465 466 467 468
	if (!cur_freq)
		return sprintf(buf, "<unknown>");
	return sprintf(buf, "%u\n", cur_freq);
}

/**
 * show_scaling_governor - show the current policy for the specified CPU
 */
D
Dave Jones 已提交
469
static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
L
Linus Torvalds 已提交
470
{
471
	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
L
Linus Torvalds 已提交
472 473 474 475
		return sprintf(buf, "powersave\n");
	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
		return sprintf(buf, "performance\n");
	else if (policy->governor)
476
		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
477
				policy->governor->name);
L
Linus Torvalds 已提交
478 479 480 481 482 483
	return -EINVAL;
}

/**
 * store_scaling_governor - store policy for the specified CPU
 */
D
Dave Jones 已提交
484 485
static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
					const char *buf, size_t count)
L
Linus Torvalds 已提交
486
{
487
	int ret;
L
Linus Torvalds 已提交
488 489 490 491 492 493 494
	char	str_governor[16];
	struct cpufreq_policy new_policy;

	ret = cpufreq_get_policy(&new_policy, policy->cpu);
	if (ret)
		return ret;

495
	ret = sscanf(buf, "%15s", str_governor);
L
Linus Torvalds 已提交
496 497 498
	if (ret != 1)
		return -EINVAL;

499 500
	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
						&new_policy.governor))
L
Linus Torvalds 已提交
501 502
		return -EINVAL;

503
	ret = cpufreq_set_policy(policy, &new_policy);
504 505 506 507

	policy->user_policy.policy = policy->policy;
	policy->user_policy.governor = policy->governor;

508 509 510 511
	if (ret)
		return ret;
	else
		return count;
L
Linus Torvalds 已提交
512 513 514 515 516
}

/**
 * show_scaling_driver - show the cpufreq driver currently loaded
 */
D
Dave Jones 已提交
517
static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
L
Linus Torvalds 已提交
518
{
519
	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
L
Linus Torvalds 已提交
520 521 522 523 524
}

/**
 * show_scaling_available_governors - show the available CPUfreq governors
 */
D
Dave Jones 已提交
525 526
static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
						char *buf)
L
Linus Torvalds 已提交
527 528 529 530
{
	ssize_t i = 0;
	struct cpufreq_governor *t;

531
	if (!has_target()) {
L
Linus Torvalds 已提交
532 533 534 535 536
		i += sprintf(buf, "performance powersave");
		goto out;
	}

	list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
537 538
		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
		    - (CPUFREQ_NAME_LEN + 2)))
L
Linus Torvalds 已提交
539
			goto out;
540
		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
L
Linus Torvalds 已提交
541
	}
542
out:
L
Linus Torvalds 已提交
543 544 545
	i += sprintf(&buf[i], "\n");
	return i;
}
546

547
ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
L
Linus Torvalds 已提交
548 549 550 551
{
	ssize_t i = 0;
	unsigned int cpu;

552
	for_each_cpu(cpu, mask) {
L
Linus Torvalds 已提交
553 554 555 556
		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))
557
			break;
L
Linus Torvalds 已提交
558 559 560 561
	}
	i += sprintf(&buf[i], "\n");
	return i;
}
562
EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
L
Linus Torvalds 已提交
563

564 565 566 567 568 569
/**
 * 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)
{
570
	return cpufreq_show_cpus(policy->related_cpus, buf);
571 572 573 574 575 576 577
}

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

581
static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
D
Dave Jones 已提交
582
					const char *buf, size_t count)
583 584 585 586
{
	unsigned int freq = 0;
	unsigned int ret;

587
	if (!policy->governor || !policy->governor->store_setspeed)
588 589 590 591 592 593 594 595 596 597 598 599 600
		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)
{
601
	if (!policy->governor || !policy->governor->show_setspeed)
602 603 604 605
		return sprintf(buf, "<unsupported>\n");

	return policy->governor->show_setspeed(policy, buf);
}
L
Linus Torvalds 已提交
606

607
/**
608
 * show_bios_limit - show the current cpufreq HW/BIOS limitation
609 610 611 612 613
 */
static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
{
	unsigned int limit;
	int ret;
614 615
	if (cpufreq_driver->bios_limit) {
		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
616 617 618 619 620 621
		if (!ret)
			return sprintf(buf, "%u\n", limit);
	}
	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
}

622 623 624 625 626 627 628 629 630 631 632 633 634 635
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);
L
Linus Torvalds 已提交
636

D
Dave Jones 已提交
637
static struct attribute *default_attrs[] = {
L
Linus Torvalds 已提交
638 639
	&cpuinfo_min_freq.attr,
	&cpuinfo_max_freq.attr,
640
	&cpuinfo_transition_latency.attr,
L
Linus Torvalds 已提交
641 642 643
	&scaling_min_freq.attr,
	&scaling_max_freq.attr,
	&affected_cpus.attr,
644
	&related_cpus.attr,
L
Linus Torvalds 已提交
645 646 647
	&scaling_governor.attr,
	&scaling_driver.attr,
	&scaling_available_governors.attr,
648
	&scaling_setspeed.attr,
L
Linus Torvalds 已提交
649 650 651
	NULL
};

652 653
#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
#define to_attr(a) container_of(a, struct freq_attr, attr)
L
Linus Torvalds 已提交
654

655
static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
L
Linus Torvalds 已提交
656
{
D
Dave Jones 已提交
657 658
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
659
	ssize_t ret;
660 661

	if (!down_read_trylock(&cpufreq_rwsem))
662
		return -EINVAL;
663

664
	down_read(&policy->rwsem);
665

666 667 668 669 670
	if (fattr->show)
		ret = fattr->show(policy, buf);
	else
		ret = -EIO;

671
	up_read(&policy->rwsem);
672
	up_read(&cpufreq_rwsem);
673

L
Linus Torvalds 已提交
674 675 676
	return ret;
}

D
Dave Jones 已提交
677 678
static ssize_t store(struct kobject *kobj, struct attribute *attr,
		     const char *buf, size_t count)
L
Linus Torvalds 已提交
679
{
D
Dave Jones 已提交
680 681
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
682
	ssize_t ret = -EINVAL;
683

684 685 686 687 688
	get_online_cpus();

	if (!cpu_online(policy->cpu))
		goto unlock;

689
	if (!down_read_trylock(&cpufreq_rwsem))
690
		goto unlock;
691

692
	down_write(&policy->rwsem);
693

694 695 696 697 698
	if (fattr->store)
		ret = fattr->store(policy, buf, count);
	else
		ret = -EIO;

699
	up_write(&policy->rwsem);
700 701

	up_read(&cpufreq_rwsem);
702 703 704
unlock:
	put_online_cpus();

L
Linus Torvalds 已提交
705 706 707
	return ret;
}

D
Dave Jones 已提交
708
static void cpufreq_sysfs_release(struct kobject *kobj)
L
Linus Torvalds 已提交
709
{
D
Dave Jones 已提交
710
	struct cpufreq_policy *policy = to_policy(kobj);
711
	pr_debug("last reference is dropped\n");
L
Linus Torvalds 已提交
712 713 714
	complete(&policy->kobj_unregister);
}

715
static const struct sysfs_ops sysfs_ops = {
L
Linus Torvalds 已提交
716 717 718 719 720 721 722 723 724 725
	.show	= show,
	.store	= store,
};

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

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
struct kobject *cpufreq_global_kobject;
EXPORT_SYMBOL(cpufreq_global_kobject);

static int cpufreq_global_kobject_usage;

int cpufreq_get_global_kobject(void)
{
	if (!cpufreq_global_kobject_usage++)
		return kobject_add(cpufreq_global_kobject,
				&cpu_subsys.dev_root->kobj, "%s", "cpufreq");

	return 0;
}
EXPORT_SYMBOL(cpufreq_get_global_kobject);

void cpufreq_put_global_kobject(void)
{
	if (!--cpufreq_global_kobject_usage)
		kobject_del(cpufreq_global_kobject);
}
EXPORT_SYMBOL(cpufreq_put_global_kobject);

int cpufreq_sysfs_create_file(const struct attribute *attr)
{
	int ret = cpufreq_get_global_kobject();

	if (!ret) {
		ret = sysfs_create_file(cpufreq_global_kobject, attr);
		if (ret)
			cpufreq_put_global_kobject();
	}

	return ret;
}
EXPORT_SYMBOL(cpufreq_sysfs_create_file);

void cpufreq_sysfs_remove_file(const struct attribute *attr)
{
	sysfs_remove_file(cpufreq_global_kobject, attr);
	cpufreq_put_global_kobject();
}
EXPORT_SYMBOL(cpufreq_sysfs_remove_file);

769
/* symlink affected CPUs */
770
static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
771 772 773 774 775
{
	unsigned int j;
	int ret = 0;

	for_each_cpu(j, policy->cpus) {
776
		struct device *cpu_dev;
777

778
		if (j == policy->cpu)
779 780
			continue;

781
		pr_debug("Adding link for CPU: %u\n", j);
782 783
		cpu_dev = get_cpu_device(j);
		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
784
					"cpufreq");
785 786
		if (ret)
			break;
787 788 789 790
	}
	return ret;
}

791
static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
792
				     struct device *dev)
793 794 795 796 797 798
{
	struct freq_attr **drv_attr;
	int ret = 0;

	/* prepare interface data */
	ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
799
				   &dev->kobj, "cpufreq");
800 801 802 803
	if (ret)
		return ret;

	/* set up files for this cpu device */
804
	drv_attr = cpufreq_driver->attr;
805 806 807
	while ((drv_attr) && (*drv_attr)) {
		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
		if (ret)
808
			goto err_out_kobj_put;
809 810
		drv_attr++;
	}
811
	if (cpufreq_driver->get) {
812 813
		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
		if (ret)
814
			goto err_out_kobj_put;
815
	}
816
	if (has_target()) {
817 818
		ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
		if (ret)
819
			goto err_out_kobj_put;
820
	}
821
	if (cpufreq_driver->bios_limit) {
822 823
		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
		if (ret)
824
			goto err_out_kobj_put;
825
	}
826

827
	ret = cpufreq_add_dev_symlink(policy);
828 829 830
	if (ret)
		goto err_out_kobj_put;

831 832 833 834 835 836 837 838 839 840 841 842 843
	return ret;

err_out_kobj_put:
	kobject_put(&policy->kobj);
	wait_for_completion(&policy->kobj_unregister);
	return ret;
}

static void cpufreq_init_policy(struct cpufreq_policy *policy)
{
	struct cpufreq_policy new_policy;
	int ret = 0;

844
	memcpy(&new_policy, policy, sizeof(*policy));
845 846 847 848 849 850

	/* Use the default policy if its valid. */
	if (cpufreq_driver->setpolicy)
		cpufreq_parse_governor(policy->governor->name,
					&new_policy.policy, NULL);

851
	/* assure that the starting sequence is run in cpufreq_set_policy */
852 853 854
	policy->governor = NULL;

	/* set default policy */
855
	ret = cpufreq_set_policy(policy, &new_policy);
856
	if (ret) {
857
		pr_debug("setting policy failed\n");
858 859
		if (cpufreq_driver->exit)
			cpufreq_driver->exit(policy);
860
	}
861 862
}

863
#ifdef CONFIG_HOTPLUG_CPU
864
static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
865
				  unsigned int cpu, struct device *dev)
866
{
867
	int ret = 0;
868 869
	unsigned long flags;

870
	if (has_target()) {
871 872 873 874 875 876
		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
		if (ret) {
			pr_err("%s: Failed to stop governor\n", __func__);
			return ret;
		}
	}
877

878
	down_write(&policy->rwsem);
V
Viresh Kumar 已提交
879

880
	write_lock_irqsave(&cpufreq_driver_lock, flags);
V
Viresh Kumar 已提交
881

882 883
	cpumask_set_cpu(cpu, policy->cpus);
	per_cpu(cpufreq_cpu_data, cpu) = policy;
884
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
885

886
	up_write(&policy->rwsem);
V
Viresh Kumar 已提交
887

888
	if (has_target()) {
889 890 891 892 893
		if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
			(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
			pr_err("%s: Failed to start governor\n", __func__);
			return ret;
		}
894
	}
895

896
	return sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
897 898
}
#endif
L
Linus Torvalds 已提交
899

900 901 902 903 904
static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
{
	struct cpufreq_policy *policy;
	unsigned long flags;

905
	read_lock_irqsave(&cpufreq_driver_lock, flags);
906 907 908

	policy = per_cpu(cpufreq_cpu_data_fallback, cpu);

909
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
910 911 912 913

	return policy;
}

914 915 916 917 918 919 920 921 922 923 924 925 926 927
static struct cpufreq_policy *cpufreq_policy_alloc(void)
{
	struct cpufreq_policy *policy;

	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;

928
	INIT_LIST_HEAD(&policy->policy_list);
929 930
	init_rwsem(&policy->rwsem);

931 932 933 934 935 936 937 938 939 940
	return policy;

err_free_cpumask:
	free_cpumask_var(policy->cpus);
err_free_policy:
	kfree(policy);

	return NULL;
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
{
	struct kobject *kobj;
	struct completion *cmp;

	down_read(&policy->rwsem);
	kobj = &policy->kobj;
	cmp = &policy->kobj_unregister;
	up_read(&policy->rwsem);
	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");
}

962 963 964 965 966 967 968
static void cpufreq_policy_free(struct cpufreq_policy *policy)
{
	free_cpumask_var(policy->related_cpus);
	free_cpumask_var(policy->cpus);
	kfree(policy);
}

969 970
static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
{
971
	if (WARN_ON(cpu == policy->cpu))
972 973
		return;

974
	down_write(&policy->rwsem);
975

976 977 978
	policy->last_cpu = policy->cpu;
	policy->cpu = cpu;

979
	up_write(&policy->rwsem);
980

981 982 983 984 985
	cpufreq_frequency_table_update_policy_cpu(policy);
	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
			CPUFREQ_UPDATE_POLICY_CPU, policy);
}

986 987
static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
			     bool frozen)
L
Linus Torvalds 已提交
988
{
989
	unsigned int j, cpu = dev->id;
990
	int ret = -ENOMEM;
L
Linus Torvalds 已提交
991 992
	struct cpufreq_policy *policy;
	unsigned long flags;
993
#ifdef CONFIG_HOTPLUG_CPU
994
	struct cpufreq_policy *tpolicy;
995
	struct cpufreq_governor *gov;
996
#endif
L
Linus Torvalds 已提交
997

998 999 1000
	if (cpu_is_offline(cpu))
		return 0;

1001
	pr_debug("adding CPU %u\n", cpu);
L
Linus Torvalds 已提交
1002 1003 1004 1005 1006 1007

#ifdef CONFIG_SMP
	/* check whether a different CPU already registered this
	 * CPU because it is in the same boat. */
	policy = cpufreq_cpu_get(cpu);
	if (unlikely(policy)) {
1008
		cpufreq_cpu_put(policy);
L
Linus Torvalds 已提交
1009 1010
		return 0;
	}
1011
#endif
1012

1013 1014 1015
	if (!down_read_trylock(&cpufreq_rwsem))
		return 0;

1016 1017
#ifdef CONFIG_HOTPLUG_CPU
	/* Check if this cpu was hot-unplugged earlier and has siblings */
1018
	read_lock_irqsave(&cpufreq_driver_lock, flags);
1019 1020
	list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
		if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
1021
			read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1022
			ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev);
1023 1024
			up_read(&cpufreq_rwsem);
			return ret;
V
Viresh Kumar 已提交
1025
		}
1026
	}
1027
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
1028 1029
#endif

1030 1031 1032 1033 1034 1035 1036
	/*
	 * Restore the saved policy when doing light-weight init and fall back
	 * to the full init if that fails.
	 */
	policy = frozen ? cpufreq_policy_restore(cpu) : NULL;
	if (!policy) {
		frozen = false;
1037
		policy = cpufreq_policy_alloc();
1038 1039 1040
		if (!policy)
			goto nomem_out;
	}
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052

	/*
	 * In the resume path, since we restore a saved policy, the assignment
	 * to policy->cpu is like an update of the existing policy, rather than
	 * the creation of a brand new one. So we need to perform this update
	 * by invoking update_policy_cpu().
	 */
	if (frozen && cpu != policy->cpu)
		update_policy_cpu(policy, cpu);
	else
		policy->cpu = cpu;

1053
	policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
1054
	cpumask_copy(policy->cpus, cpumask_of(cpu));
L
Linus Torvalds 已提交
1055 1056

	init_completion(&policy->kobj_unregister);
1057
	INIT_WORK(&policy->update, handle_update);
L
Linus Torvalds 已提交
1058 1059 1060 1061

	/* call driver. From then on the cpufreq must be able
	 * to accept all calls to ->verify and ->setpolicy for this CPU
	 */
1062
	ret = cpufreq_driver->init(policy);
L
Linus Torvalds 已提交
1063
	if (ret) {
1064
		pr_debug("initialization failed\n");
V
Viresh Kumar 已提交
1065
		goto err_set_policy_cpu;
L
Linus Torvalds 已提交
1066
	}
V
Viresh Kumar 已提交
1067

1068 1069 1070 1071 1072 1073 1074 1075
	if (cpufreq_driver->get) {
		policy->cur = cpufreq_driver->get(policy->cpu);
		if (!policy->cur) {
			pr_err("%s: ->get() failed\n", __func__);
			goto err_get_freq;
		}
	}

1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115
	/*
	 * 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);
		}
	}

1116 1117 1118
	/* related cpus should atleast have policy->cpus */
	cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);

V
Viresh Kumar 已提交
1119 1120 1121 1122 1123 1124
	/*
	 * 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);

1125 1126 1127 1128
	if (!frozen) {
		policy->user_policy.min = policy->min;
		policy->user_policy.max = policy->max;
	}
L
Linus Torvalds 已提交
1129

1130 1131 1132
	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
				     CPUFREQ_START, policy);

1133 1134 1135 1136 1137 1138
#ifdef CONFIG_HOTPLUG_CPU
	gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
	if (gov) {
		policy->governor = gov;
		pr_debug("Restoring governor %s for cpu %d\n",
		       policy->governor->name, cpu);
1139
	}
1140
#endif
L
Linus Torvalds 已提交
1141

1142
	write_lock_irqsave(&cpufreq_driver_lock, flags);
1143
	for_each_cpu(j, policy->cpus)
1144 1145 1146
		per_cpu(cpufreq_cpu_data, j) = policy;
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);

1147
	if (!frozen) {
1148
		ret = cpufreq_add_dev_interface(policy, dev);
1149 1150 1151
		if (ret)
			goto err_out_unregister;
	}
1152

1153 1154 1155 1156
	write_lock_irqsave(&cpufreq_driver_lock, flags);
	list_add(&policy->policy_list, &cpufreq_policy_list);
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);

1157 1158
	cpufreq_init_policy(policy);

1159 1160 1161 1162 1163
	if (!frozen) {
		policy->user_policy.policy = policy->policy;
		policy->user_policy.governor = policy->governor;
	}

1164
	kobject_uevent(&policy->kobj, KOBJ_ADD);
1165 1166
	up_read(&cpufreq_rwsem);

1167
	pr_debug("initialization complete\n");
1168

L
Linus Torvalds 已提交
1169 1170 1171
	return 0;

err_out_unregister:
1172
	write_lock_irqsave(&cpufreq_driver_lock, flags);
1173
	for_each_cpu(j, policy->cpus)
1174
		per_cpu(cpufreq_cpu_data, j) = NULL;
1175
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
1176

1177 1178 1179
err_get_freq:
	if (cpufreq_driver->exit)
		cpufreq_driver->exit(policy);
V
Viresh Kumar 已提交
1180
err_set_policy_cpu:
1181 1182 1183
	if (frozen) {
		/* Do not leave stale fallback data behind. */
		per_cpu(cpufreq_cpu_data_fallback, cpu) = NULL;
1184
		cpufreq_policy_put_kobj(policy);
1185
	}
1186
	cpufreq_policy_free(policy);
1187

L
Linus Torvalds 已提交
1188
nomem_out:
1189 1190
	up_read(&cpufreq_rwsem);

L
Linus Torvalds 已提交
1191 1192 1193
	return ret;
}

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
/**
 * cpufreq_add_dev - add a CPU device
 *
 * Adds the cpufreq interface for a CPU device.
 *
 * The Oracle says: try running cpufreq registration/unregistration concurrently
 * with with cpu hotplugging and all hell will break loose. Tried to clean this
 * mess up, but more thorough testing is needed. - Mathieu
 */
static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
{
	return __cpufreq_add_dev(dev, sif, false);
}

1208
static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
1209
					   unsigned int old_cpu)
1210 1211 1212 1213 1214
{
	struct device *cpu_dev;
	int ret;

	/* first sibling now owns the new sysfs dir */
1215
	cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
1216

1217
	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
1218
	ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
1219 1220 1221
	if (ret) {
		pr_err("%s: Failed to move kobj: %d", __func__, ret);

1222
		down_write(&policy->rwsem);
1223
		cpumask_set_cpu(old_cpu, policy->cpus);
1224
		up_write(&policy->rwsem);
1225

1226
		ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
1227 1228 1229 1230 1231 1232 1233 1234
					"cpufreq");

		return -EINVAL;
	}

	return cpu_dev->id;
}

1235 1236 1237
static int __cpufreq_remove_dev_prepare(struct device *dev,
					struct subsys_interface *sif,
					bool frozen)
L
Linus Torvalds 已提交
1238
{
1239
	unsigned int cpu = dev->id, cpus;
1240
	int new_cpu, ret;
L
Linus Torvalds 已提交
1241
	unsigned long flags;
1242
	struct cpufreq_policy *policy;
L
Linus Torvalds 已提交
1243

1244
	pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
L
Linus Torvalds 已提交
1245

1246
	write_lock_irqsave(&cpufreq_driver_lock, flags);
V
Viresh Kumar 已提交
1247

1248
	policy = per_cpu(cpufreq_cpu_data, cpu);
V
Viresh Kumar 已提交
1249

1250 1251
	/* Save the policy somewhere when doing a light-weight tear-down */
	if (frozen)
1252
		per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
1253

1254
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
1255

1256
	if (!policy) {
1257
		pr_debug("%s: No cpu_data found\n", __func__);
L
Linus Torvalds 已提交
1258 1259 1260
		return -EINVAL;
	}

1261
	if (has_target()) {
1262 1263 1264 1265 1266 1267
		ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
		if (ret) {
			pr_err("%s: Failed to stop governor\n", __func__);
			return ret;
		}
	}
L
Linus Torvalds 已提交
1268

1269
#ifdef CONFIG_HOTPLUG_CPU
1270
	if (!cpufreq_driver->setpolicy)
1271
		strncpy(per_cpu(cpufreq_cpu_governor, cpu),
1272
			policy->governor->name, CPUFREQ_NAME_LEN);
L
Linus Torvalds 已提交
1273 1274
#endif

1275
	down_read(&policy->rwsem);
1276
	cpus = cpumask_weight(policy->cpus);
1277
	up_read(&policy->rwsem);
1278

1279 1280 1281
	if (cpu != policy->cpu) {
		if (!frozen)
			sysfs_remove_link(&dev->kobj, "cpufreq");
1282
	} else if (cpus > 1) {
1283
		new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1284
		if (new_cpu >= 0) {
1285
			update_policy_cpu(policy, new_cpu);
1286 1287

			if (!frozen) {
1288 1289
				pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
						__func__, new_cpu, cpu);
1290
			}
L
Linus Torvalds 已提交
1291 1292 1293
		}
	}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	return 0;
}

static int __cpufreq_remove_dev_finish(struct device *dev,
				       struct subsys_interface *sif,
				       bool frozen)
{
	unsigned int cpu = dev->id, cpus;
	int ret;
	unsigned long flags;
	struct cpufreq_policy *policy;

	read_lock_irqsave(&cpufreq_driver_lock, flags);
	policy = per_cpu(cpufreq_cpu_data, cpu);
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);

	if (!policy) {
		pr_debug("%s: No cpu_data found\n", __func__);
		return -EINVAL;
	}

1315
	down_write(&policy->rwsem);
1316
	cpus = cpumask_weight(policy->cpus);
1317 1318 1319

	if (cpus > 1)
		cpumask_clear_cpu(cpu, policy->cpus);
1320
	up_write(&policy->rwsem);
1321

1322 1323
	/* If cpu is last user of policy, free policy */
	if (cpus == 1) {
1324
		if (has_target()) {
1325 1326 1327 1328 1329 1330 1331
			ret = __cpufreq_governor(policy,
					CPUFREQ_GOV_POLICY_EXIT);
			if (ret) {
				pr_err("%s: Failed to exit governor\n",
						__func__);
				return ret;
			}
1332
		}
1333

1334 1335
		if (!frozen)
			cpufreq_policy_put_kobj(policy);
1336

1337 1338 1339 1340
		/*
		 * Perform the ->exit() even during light-weight tear-down,
		 * since this is a core component, and is essential for the
		 * subsequent light-weight ->init() to succeed.
1341
		 */
1342
		if (cpufreq_driver->exit)
1343
			cpufreq_driver->exit(policy);
1344

1345 1346 1347 1348 1349
		/* Remove policy from list of active policies */
		write_lock_irqsave(&cpufreq_driver_lock, flags);
		list_del(&policy->policy_list);
		write_unlock_irqrestore(&cpufreq_driver_lock, flags);

1350
		if (!frozen)
1351
			cpufreq_policy_free(policy);
1352
	} else {
1353
		if (has_target()) {
1354 1355 1356 1357 1358 1359
			if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
					(ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
				pr_err("%s: Failed to start governor\n",
						__func__);
				return ret;
			}
1360
		}
1361
	}
L
Linus Torvalds 已提交
1362

1363
	per_cpu(cpufreq_cpu_data, cpu) = NULL;
L
Linus Torvalds 已提交
1364 1365 1366
	return 0;
}

1367
/**
1368
 * cpufreq_remove_dev - remove a CPU device
1369 1370 1371
 *
 * Removes the cpufreq interface for a CPU device.
 */
1372
static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
1373
{
1374
	unsigned int cpu = dev->id;
1375
	int ret;
1376 1377 1378 1379

	if (cpu_is_offline(cpu))
		return 0;

1380 1381 1382 1383 1384 1385
	ret = __cpufreq_remove_dev_prepare(dev, sif, false);

	if (!ret)
		ret = __cpufreq_remove_dev_finish(dev, sif, false);

	return ret;
1386 1387
}

1388
static void handle_update(struct work_struct *work)
L
Linus Torvalds 已提交
1389
{
1390 1391 1392
	struct cpufreq_policy *policy =
		container_of(work, struct cpufreq_policy, update);
	unsigned int cpu = policy->cpu;
1393
	pr_debug("handle_update for cpu %u called\n", cpu);
L
Linus Torvalds 已提交
1394 1395 1396 1397
	cpufreq_update_policy(cpu);
}

/**
1398 1399
 *	cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
 *	in deep trouble.
L
Linus Torvalds 已提交
1400 1401 1402 1403
 *	@cpu: cpu number
 *	@old_freq: CPU frequency the kernel thinks the CPU runs at
 *	@new_freq: CPU frequency the CPU actually runs at
 *
1404 1405
 *	We adjust to current frequency first, and need to clean up later.
 *	So either call to cpufreq_update_policy() or schedule handle_update()).
L
Linus Torvalds 已提交
1406
 */
1407 1408
static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
				unsigned int new_freq)
L
Linus Torvalds 已提交
1409
{
1410
	struct cpufreq_policy *policy;
L
Linus Torvalds 已提交
1411
	struct cpufreq_freqs freqs;
1412 1413
	unsigned long flags;

1414
	pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
L
Linus Torvalds 已提交
1415 1416 1417 1418
	       "core thinks of %u, is %u kHz.\n", old_freq, new_freq);

	freqs.old = old_freq;
	freqs.new = new_freq;
1419 1420 1421 1422 1423 1424 1425

	read_lock_irqsave(&cpufreq_driver_lock, flags);
	policy = per_cpu(cpufreq_cpu_data, cpu);
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);

	cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
	cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
L
Linus Torvalds 已提交
1426 1427
}

1428
/**
D
Dhaval Giani 已提交
1429
 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
1430 1431 1432 1433 1434 1435 1436
 * @cpu: CPU number
 *
 * This is the last known freq, without actually getting it from the driver.
 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
 */
unsigned int cpufreq_quick_get(unsigned int cpu)
{
1437
	struct cpufreq_policy *policy;
1438
	unsigned int ret_freq = 0;
1439

1440 1441
	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
		return cpufreq_driver->get(cpu);
1442 1443

	policy = cpufreq_cpu_get(cpu);
1444
	if (policy) {
1445
		ret_freq = policy->cur;
1446 1447 1448
		cpufreq_cpu_put(policy);
	}

D
Dave Jones 已提交
1449
	return ret_freq;
1450 1451 1452
}
EXPORT_SYMBOL(cpufreq_quick_get);

1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472
/**
 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
 * @cpu: CPU number
 *
 * Just return the max possible frequency for a given CPU.
 */
unsigned int cpufreq_quick_get_max(unsigned int cpu)
{
	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
	unsigned int ret_freq = 0;

	if (policy) {
		ret_freq = policy->max;
		cpufreq_cpu_put(policy);
	}

	return ret_freq;
}
EXPORT_SYMBOL(cpufreq_quick_get_max);

1473
static unsigned int __cpufreq_get(unsigned int cpu)
L
Linus Torvalds 已提交
1474
{
1475
	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1476
	unsigned int ret_freq = 0;
1477

1478
	if (!cpufreq_driver->get)
D
Dave Jones 已提交
1479
		return ret_freq;
L
Linus Torvalds 已提交
1480

1481
	ret_freq = cpufreq_driver->get(cpu);
L
Linus Torvalds 已提交
1482

1483
	if (ret_freq && policy->cur &&
1484
		!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
1485 1486 1487 1488
		/* verify no discrepancy between actual and
					saved value exists */
		if (unlikely(ret_freq != policy->cur)) {
			cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
L
Linus Torvalds 已提交
1489 1490 1491 1492
			schedule_work(&policy->update);
		}
	}

D
Dave Jones 已提交
1493
	return ret_freq;
1494
}
L
Linus Torvalds 已提交
1495

1496 1497 1498 1499 1500 1501 1502 1503
/**
 * cpufreq_get - get the current CPU frequency (in kHz)
 * @cpu: CPU number
 *
 * Get the CPU current (static) CPU frequency
 */
unsigned int cpufreq_get(unsigned int cpu)
{
1504
	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
1505 1506
	unsigned int ret_freq = 0;

1507 1508 1509
	if (cpufreq_disabled() || !cpufreq_driver)
		return -ENOENT;

1510 1511
	BUG_ON(!policy);

1512 1513
	if (!down_read_trylock(&cpufreq_rwsem))
		return 0;
1514

1515
	down_read(&policy->rwsem);
1516 1517 1518

	ret_freq = __cpufreq_get(cpu);

1519
	up_read(&policy->rwsem);
1520 1521
	up_read(&cpufreq_rwsem);

D
Dave Jones 已提交
1522
	return ret_freq;
L
Linus Torvalds 已提交
1523 1524 1525
}
EXPORT_SYMBOL(cpufreq_get);

1526 1527 1528 1529 1530
static struct subsys_interface cpufreq_interface = {
	.name		= "cpufreq",
	.subsys		= &cpu_subsys,
	.add_dev	= cpufreq_add_dev,
	.remove_dev	= cpufreq_remove_dev,
1531 1532
};

1533
/**
1534 1535 1536 1537
 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
 *
 * This function is only executed for the boot processor.  The other CPUs
 * have been put offline by means of CPU hotplug.
1538
 */
1539
static int cpufreq_bp_suspend(void)
1540
{
1541
	int ret = 0;
1542

1543
	int cpu = smp_processor_id();
1544
	struct cpufreq_policy *policy;
1545

1546
	pr_debug("suspending cpu %u\n", cpu);
1547

1548
	/* If there's no policy for the boot CPU, we have nothing to do. */
1549 1550
	policy = cpufreq_cpu_get(cpu);
	if (!policy)
1551
		return 0;
1552

1553
	if (cpufreq_driver->suspend) {
1554
		ret = cpufreq_driver->suspend(policy);
1555
		if (ret)
1556
			printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
1557
					"step on CPU %u\n", policy->cpu);
1558 1559
	}

1560
	cpufreq_cpu_put(policy);
1561
	return ret;
1562 1563
}

L
Linus Torvalds 已提交
1564
/**
1565
 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
L
Linus Torvalds 已提交
1566 1567
 *
 *	1.) resume CPUfreq hardware support (cpufreq_driver->resume())
1568 1569 1570 1571 1572
 *	2.) schedule call cpufreq_update_policy() ASAP as interrupts are
 *	    restored. It will verify that the current freq is in sync with
 *	    what we believe it to be. This is a bit later than when it
 *	    should be, but nonethteless it's better than calling
 *	    cpufreq_driver->get() here which might re-enable interrupts...
1573 1574 1575
 *
 * This function is only executed for the boot CPU.  The other CPUs have not
 * been turned on yet.
L
Linus Torvalds 已提交
1576
 */
1577
static void cpufreq_bp_resume(void)
L
Linus Torvalds 已提交
1578
{
1579
	int ret = 0;
1580

1581
	int cpu = smp_processor_id();
1582
	struct cpufreq_policy *policy;
L
Linus Torvalds 已提交
1583

1584
	pr_debug("resuming cpu %u\n", cpu);
L
Linus Torvalds 已提交
1585

1586
	/* If there's no policy for the boot CPU, we have nothing to do. */
1587 1588
	policy = cpufreq_cpu_get(cpu);
	if (!policy)
1589
		return;
L
Linus Torvalds 已提交
1590

1591
	if (cpufreq_driver->resume) {
1592
		ret = cpufreq_driver->resume(policy);
L
Linus Torvalds 已提交
1593 1594
		if (ret) {
			printk(KERN_ERR "cpufreq: resume failed in ->resume "
1595
					"step on CPU %u\n", policy->cpu);
1596
			goto fail;
L
Linus Torvalds 已提交
1597 1598 1599
		}
	}

1600
	schedule_work(&policy->update);
1601

1602
fail:
1603
	cpufreq_cpu_put(policy);
L
Linus Torvalds 已提交
1604 1605
}

1606 1607 1608
static struct syscore_ops cpufreq_syscore_ops = {
	.suspend	= cpufreq_bp_suspend,
	.resume		= cpufreq_bp_resume,
L
Linus Torvalds 已提交
1609 1610
};

1611 1612 1613 1614 1615 1616 1617 1618
/**
 *	cpufreq_get_current_driver - return current driver's name
 *
 *	Return the name string of the currently loaded cpufreq driver
 *	or NULL, if none.
 */
const char *cpufreq_get_current_driver(void)
{
1619 1620 1621 1622
	if (cpufreq_driver)
		return cpufreq_driver->name;

	return NULL;
1623 1624
}
EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
L
Linus Torvalds 已提交
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634

/*********************************************************************
 *                     NOTIFIER LISTS INTERFACE                      *
 *********************************************************************/

/**
 *	cpufreq_register_notifier - register a driver with cpufreq
 *	@nb: notifier function to register
 *      @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
 *
1635
 *	Add a driver to one of two lists: either a list of drivers that
L
Linus Torvalds 已提交
1636 1637 1638 1639 1640
 *      are notified about clock rate changes (once before and once after
 *      the transition), or a list of drivers that are notified about
 *      changes in cpufreq policy.
 *
 *	This function may sleep, and has the same return conditions as
1641
 *	blocking_notifier_chain_register.
L
Linus Torvalds 已提交
1642 1643 1644 1645 1646
 */
int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
{
	int ret;

1647 1648 1649
	if (cpufreq_disabled())
		return -EINVAL;

1650 1651
	WARN_ON(!init_cpufreq_transition_notifier_list_called);

L
Linus Torvalds 已提交
1652 1653
	switch (list) {
	case CPUFREQ_TRANSITION_NOTIFIER:
1654
		ret = srcu_notifier_chain_register(
1655
				&cpufreq_transition_notifier_list, nb);
L
Linus Torvalds 已提交
1656 1657
		break;
	case CPUFREQ_POLICY_NOTIFIER:
1658 1659
		ret = blocking_notifier_chain_register(
				&cpufreq_policy_notifier_list, nb);
L
Linus Torvalds 已提交
1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}
EXPORT_SYMBOL(cpufreq_register_notifier);

/**
 *	cpufreq_unregister_notifier - unregister a driver with cpufreq
 *	@nb: notifier block to be unregistered
1672
 *	@list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
L
Linus Torvalds 已提交
1673 1674 1675 1676
 *
 *	Remove a driver from the CPU frequency notifier list.
 *
 *	This function may sleep, and has the same return conditions as
1677
 *	blocking_notifier_chain_unregister.
L
Linus Torvalds 已提交
1678 1679 1680 1681 1682
 */
int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
{
	int ret;

1683 1684 1685
	if (cpufreq_disabled())
		return -EINVAL;

L
Linus Torvalds 已提交
1686 1687
	switch (list) {
	case CPUFREQ_TRANSITION_NOTIFIER:
1688
		ret = srcu_notifier_chain_unregister(
1689
				&cpufreq_transition_notifier_list, nb);
L
Linus Torvalds 已提交
1690 1691
		break;
	case CPUFREQ_POLICY_NOTIFIER:
1692 1693
		ret = blocking_notifier_chain_unregister(
				&cpufreq_policy_notifier_list, nb);
L
Linus Torvalds 已提交
1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
		break;
	default:
		ret = -EINVAL;
	}

	return ret;
}
EXPORT_SYMBOL(cpufreq_unregister_notifier);


/*********************************************************************
 *                              GOVERNORS                            *
 *********************************************************************/

int __cpufreq_driver_target(struct cpufreq_policy *policy,
			    unsigned int target_freq,
			    unsigned int relation)
{
	int retval = -EINVAL;
1713
	unsigned int old_target_freq = target_freq;
1714

1715 1716 1717
	if (cpufreq_disabled())
		return -ENODEV;

1718 1719 1720 1721 1722 1723 1724 1725
	/* Make sure that target_freq is within supported range */
	if (target_freq > policy->max)
		target_freq = policy->max;
	if (target_freq < policy->min)
		target_freq = policy->min;

	pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
			policy->cpu, target_freq, relation, old_target_freq);
1726

1727 1728 1729 1730 1731 1732
	/*
	 * This might look like a redundant call as we are checking it again
	 * after finding index. But it is left intentionally for cases where
	 * exactly same freq is called again and so we can save on few function
	 * calls.
	 */
1733 1734 1735
	if (target_freq == policy->cur)
		return 0;

1736 1737
	if (cpufreq_driver->target)
		retval = cpufreq_driver->target(policy, target_freq, relation);
1738 1739
	else if (cpufreq_driver->target_index) {
		struct cpufreq_frequency_table *freq_table;
1740 1741
		struct cpufreq_freqs freqs;
		bool notify;
1742
		int index;
1743

1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756
		freq_table = cpufreq_frequency_get_table(policy->cpu);
		if (unlikely(!freq_table)) {
			pr_err("%s: Unable to find freq_table\n", __func__);
			goto out;
		}

		retval = cpufreq_frequency_table_target(policy, freq_table,
				target_freq, relation, &index);
		if (unlikely(retval)) {
			pr_err("%s: Unable to find matching freq\n", __func__);
			goto out;
		}

1757
		if (freq_table[index].frequency == policy->cur) {
1758
			retval = 0;
1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			goto out;
		}

		notify = !(cpufreq_driver->flags & CPUFREQ_ASYNC_NOTIFICATION);

		if (notify) {
			freqs.old = policy->cur;
			freqs.new = freq_table[index].frequency;
			freqs.flags = 0;

			pr_debug("%s: cpu: %d, oldfreq: %u, new freq: %u\n",
					__func__, policy->cpu, freqs.old,
					freqs.new);

			cpufreq_notify_transition(policy, &freqs,
					CPUFREQ_PRECHANGE);
		}

		retval = cpufreq_driver->target_index(policy, index);
		if (retval)
			pr_err("%s: Failed to change cpu frequency: %d\n",
					__func__, retval);

1782 1783
		if (notify)
			cpufreq_notify_post_transition(policy, &freqs, retval);
1784 1785 1786
	}

out:
L
Linus Torvalds 已提交
1787 1788 1789 1790 1791 1792 1793 1794
	return retval;
}
EXPORT_SYMBOL_GPL(__cpufreq_driver_target);

int cpufreq_driver_target(struct cpufreq_policy *policy,
			  unsigned int target_freq,
			  unsigned int relation)
{
1795
	int ret = -EINVAL;
L
Linus Torvalds 已提交
1796

1797
	down_write(&policy->rwsem);
L
Linus Torvalds 已提交
1798 1799 1800

	ret = __cpufreq_driver_target(policy, target_freq, relation);

1801
	up_write(&policy->rwsem);
L
Linus Torvalds 已提交
1802 1803 1804 1805 1806

	return ret;
}
EXPORT_SYMBOL_GPL(cpufreq_driver_target);

1807 1808 1809
/*
 * when "event" is CPUFREQ_GOV_LIMITS
 */
L
Linus Torvalds 已提交
1810

1811 1812
static int __cpufreq_governor(struct cpufreq_policy *policy,
					unsigned int event)
L
Linus Torvalds 已提交
1813
{
1814
	int ret;
1815 1816 1817 1818 1819 1820 1821 1822 1823 1824

	/* Only must be defined when default governor is known to have latency
	   restrictions, like e.g. conservative or ondemand.
	   That this is the case is already ensured in Kconfig
	*/
#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
	struct cpufreq_governor *gov = &cpufreq_gov_performance;
#else
	struct cpufreq_governor *gov = NULL;
#endif
1825 1826 1827 1828

	if (policy->governor->max_transition_latency &&
	    policy->cpuinfo.transition_latency >
	    policy->governor->max_transition_latency) {
1829 1830 1831 1832 1833 1834 1835 1836 1837 1838
		if (!gov)
			return -EINVAL;
		else {
			printk(KERN_WARNING "%s governor failed, too long"
			       " transition latency of HW, fallback"
			       " to %s governor\n",
			       policy->governor->name,
			       gov->name);
			policy->governor = gov;
		}
1839
	}
L
Linus Torvalds 已提交
1840

1841 1842 1843
	if (event == CPUFREQ_GOV_POLICY_INIT)
		if (!try_module_get(policy->governor->owner))
			return -EINVAL;
L
Linus Torvalds 已提交
1844

1845
	pr_debug("__cpufreq_governor for CPU %u, event %u\n",
1846
						policy->cpu, event);
1847 1848

	mutex_lock(&cpufreq_governor_lock);
1849
	if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
1850 1851
	    || (!policy->governor_enabled
	    && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
		mutex_unlock(&cpufreq_governor_lock);
		return -EBUSY;
	}

	if (event == CPUFREQ_GOV_STOP)
		policy->governor_enabled = false;
	else if (event == CPUFREQ_GOV_START)
		policy->governor_enabled = true;

	mutex_unlock(&cpufreq_governor_lock);

L
Linus Torvalds 已提交
1863 1864
	ret = policy->governor->governor(policy, event);

1865 1866 1867 1868 1869
	if (!ret) {
		if (event == CPUFREQ_GOV_POLICY_INIT)
			policy->governor->initialized++;
		else if (event == CPUFREQ_GOV_POLICY_EXIT)
			policy->governor->initialized--;
1870 1871 1872 1873 1874 1875 1876 1877
	} else {
		/* Restore original values */
		mutex_lock(&cpufreq_governor_lock);
		if (event == CPUFREQ_GOV_STOP)
			policy->governor_enabled = true;
		else if (event == CPUFREQ_GOV_START)
			policy->governor_enabled = false;
		mutex_unlock(&cpufreq_governor_lock);
1878
	}
1879

1880 1881
	if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
			((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
L
Linus Torvalds 已提交
1882 1883 1884 1885 1886 1887 1888
		module_put(policy->governor->owner);

	return ret;
}

int cpufreq_register_governor(struct cpufreq_governor *governor)
{
1889
	int err;
L
Linus Torvalds 已提交
1890 1891 1892 1893

	if (!governor)
		return -EINVAL;

1894 1895 1896
	if (cpufreq_disabled())
		return -ENODEV;

1897
	mutex_lock(&cpufreq_governor_mutex);
1898

1899
	governor->initialized = 0;
1900 1901 1902 1903
	err = -EBUSY;
	if (__find_governor(governor->name) == NULL) {
		err = 0;
		list_add(&governor->governor_list, &cpufreq_governor_list);
L
Linus Torvalds 已提交
1904 1905
	}

1906
	mutex_unlock(&cpufreq_governor_mutex);
1907
	return err;
L
Linus Torvalds 已提交
1908 1909 1910 1911 1912
}
EXPORT_SYMBOL_GPL(cpufreq_register_governor);

void cpufreq_unregister_governor(struct cpufreq_governor *governor)
{
1913 1914 1915 1916
#ifdef CONFIG_HOTPLUG_CPU
	int cpu;
#endif

L
Linus Torvalds 已提交
1917 1918 1919
	if (!governor)
		return;

1920 1921 1922
	if (cpufreq_disabled())
		return;

1923 1924 1925 1926 1927 1928 1929 1930 1931
#ifdef CONFIG_HOTPLUG_CPU
	for_each_present_cpu(cpu) {
		if (cpu_online(cpu))
			continue;
		if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
			strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
	}
#endif

1932
	mutex_lock(&cpufreq_governor_mutex);
L
Linus Torvalds 已提交
1933
	list_del(&governor->governor_list);
1934
	mutex_unlock(&cpufreq_governor_mutex);
L
Linus Torvalds 已提交
1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945
	return;
}
EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);


/*********************************************************************
 *                          POLICY INTERFACE                         *
 *********************************************************************/

/**
 * cpufreq_get_policy - get the current cpufreq_policy
1946 1947
 * @policy: struct cpufreq_policy into which the current cpufreq_policy
 *	is written
L
Linus Torvalds 已提交
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960
 *
 * Reads the current cpufreq policy.
 */
int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
{
	struct cpufreq_policy *cpu_policy;
	if (!policy)
		return -EINVAL;

	cpu_policy = cpufreq_cpu_get(cpu);
	if (!cpu_policy)
		return -EINVAL;

1961
	memcpy(policy, cpu_policy, sizeof(*policy));
L
Linus Torvalds 已提交
1962 1963 1964 1965 1966 1967

	cpufreq_cpu_put(cpu_policy);
	return 0;
}
EXPORT_SYMBOL(cpufreq_get_policy);

1968
/*
1969 1970
 * policy : current policy.
 * new_policy: policy to be set.
1971
 */
1972
static int cpufreq_set_policy(struct cpufreq_policy *policy,
1973
				struct cpufreq_policy *new_policy)
L
Linus Torvalds 已提交
1974
{
1975
	int ret = 0, failed = 1;
L
Linus Torvalds 已提交
1976

1977 1978
	pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
		new_policy->min, new_policy->max);
L
Linus Torvalds 已提交
1979

1980
	memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
L
Linus Torvalds 已提交
1981

1982
	if (new_policy->min > policy->max || new_policy->max < policy->min) {
1983 1984 1985 1986
		ret = -EINVAL;
		goto error_out;
	}

L
Linus Torvalds 已提交
1987
	/* verify the cpu speed can be set within this limit */
1988
	ret = cpufreq_driver->verify(new_policy);
L
Linus Torvalds 已提交
1989 1990 1991 1992
	if (ret)
		goto error_out;

	/* adjust if necessary - all reasons */
1993
	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1994
			CPUFREQ_ADJUST, new_policy);
L
Linus Torvalds 已提交
1995 1996

	/* adjust if necessary - hardware incompatibility*/
1997
	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1998
			CPUFREQ_INCOMPATIBLE, new_policy);
L
Linus Torvalds 已提交
1999

2000 2001 2002 2003
	/*
	 * verify the cpu speed can be set within this limit, which might be
	 * different to the first one
	 */
2004
	ret = cpufreq_driver->verify(new_policy);
2005
	if (ret)
L
Linus Torvalds 已提交
2006 2007 2008
		goto error_out;

	/* notification of the new policy */
2009
	blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
2010
			CPUFREQ_NOTIFY, new_policy);
L
Linus Torvalds 已提交
2011

2012 2013
	policy->min = new_policy->min;
	policy->max = new_policy->max;
L
Linus Torvalds 已提交
2014

2015
	pr_debug("new min and max freqs are %u - %u kHz\n",
2016
					policy->min, policy->max);
L
Linus Torvalds 已提交
2017

2018
	if (cpufreq_driver->setpolicy) {
2019
		policy->policy = new_policy->policy;
2020
		pr_debug("setting range\n");
2021
		ret = cpufreq_driver->setpolicy(new_policy);
L
Linus Torvalds 已提交
2022
	} else {
2023
		if (new_policy->governor != policy->governor) {
L
Linus Torvalds 已提交
2024
			/* save old, working values */
2025
			struct cpufreq_governor *old_gov = policy->governor;
L
Linus Torvalds 已提交
2026

2027
			pr_debug("governor switch\n");
L
Linus Torvalds 已提交
2028 2029

			/* end old governor */
2030 2031
			if (policy->governor) {
				__cpufreq_governor(policy, CPUFREQ_GOV_STOP);
2032
				up_write(&policy->rwsem);
2033
				__cpufreq_governor(policy,
2034
						CPUFREQ_GOV_POLICY_EXIT);
2035
				down_write(&policy->rwsem);
2036
			}
L
Linus Torvalds 已提交
2037 2038

			/* start new governor */
2039 2040 2041
			policy->governor = new_policy->governor;
			if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
				if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
2042
					failed = 0;
2043
				} else {
2044
					up_write(&policy->rwsem);
2045
					__cpufreq_governor(policy,
2046
							CPUFREQ_GOV_POLICY_EXIT);
2047
					down_write(&policy->rwsem);
2048
				}
2049 2050 2051
			}

			if (failed) {
L
Linus Torvalds 已提交
2052
				/* new governor failed, so re-start old one */
2053
				pr_debug("starting governor %s failed\n",
2054
							policy->governor->name);
L
Linus Torvalds 已提交
2055
				if (old_gov) {
2056 2057
					policy->governor = old_gov;
					__cpufreq_governor(policy,
2058
							CPUFREQ_GOV_POLICY_INIT);
2059
					__cpufreq_governor(policy,
2060
							   CPUFREQ_GOV_START);
L
Linus Torvalds 已提交
2061 2062 2063 2064 2065 2066
				}
				ret = -EINVAL;
				goto error_out;
			}
			/* might be a policy change, too, so fall through */
		}
2067
		pr_debug("governor: change or update limits\n");
2068
		ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
L
Linus Torvalds 已提交
2069 2070
	}

2071
error_out:
L
Linus Torvalds 已提交
2072 2073 2074 2075 2076 2077 2078
	return ret;
}

/**
 *	cpufreq_update_policy - re-evaluate an existing cpufreq policy
 *	@cpu: CPU which shall be re-evaluated
 *
L
Lucas De Marchi 已提交
2079
 *	Useful for policy notifiers which have different necessities
L
Linus Torvalds 已提交
2080 2081 2082 2083
 *	at different times.
 */
int cpufreq_update_policy(unsigned int cpu)
{
2084 2085
	struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
	struct cpufreq_policy new_policy;
2086
	int ret;
L
Linus Torvalds 已提交
2087

2088
	if (!policy) {
2089 2090 2091
		ret = -ENODEV;
		goto no_policy;
	}
L
Linus Torvalds 已提交
2092

2093
	down_write(&policy->rwsem);
L
Linus Torvalds 已提交
2094

2095
	pr_debug("updating policy for CPU %u\n", cpu);
2096
	memcpy(&new_policy, policy, sizeof(*policy));
2097 2098 2099 2100
	new_policy.min = policy->user_policy.min;
	new_policy.max = policy->user_policy.max;
	new_policy.policy = policy->user_policy.policy;
	new_policy.governor = policy->user_policy.governor;
L
Linus Torvalds 已提交
2101

2102 2103 2104 2105
	/*
	 * BIOS might change freq behind our back
	 * -> ask driver for current freq and notify governors about a change
	 */
2106
	if (cpufreq_driver->get) {
2107 2108
		new_policy.cur = cpufreq_driver->get(cpu);
		if (!policy->cur) {
2109
			pr_debug("Driver did not initialize current freq");
2110
			policy->cur = new_policy.cur;
2111
		} else {
2112
			if (policy->cur != new_policy.cur && has_target())
2113 2114
				cpufreq_out_of_sync(cpu, policy->cur,
								new_policy.cur);
2115
		}
2116 2117
	}

2118
	ret = cpufreq_set_policy(policy, &new_policy);
L
Linus Torvalds 已提交
2119

2120
	up_write(&policy->rwsem);
2121

2122
	cpufreq_cpu_put(policy);
2123
no_policy:
L
Linus Torvalds 已提交
2124 2125 2126 2127
	return ret;
}
EXPORT_SYMBOL(cpufreq_update_policy);

2128
static int cpufreq_cpu_callback(struct notifier_block *nfb,
2129 2130 2131
					unsigned long action, void *hcpu)
{
	unsigned int cpu = (unsigned long)hcpu;
2132
	struct device *dev;
2133
	bool frozen = false;
2134

2135 2136
	dev = get_cpu_device(cpu);
	if (dev) {
2137

2138 2139 2140
		if (action & CPU_TASKS_FROZEN)
			frozen = true;

2141
		switch (action & ~CPU_TASKS_FROZEN) {
2142
		case CPU_ONLINE:
2143
			__cpufreq_add_dev(dev, NULL, frozen);
2144
			cpufreq_update_policy(cpu);
2145
			break;
2146

2147
		case CPU_DOWN_PREPARE:
2148
			__cpufreq_remove_dev_prepare(dev, NULL, frozen);
2149 2150 2151
			break;

		case CPU_POST_DEAD:
2152
			__cpufreq_remove_dev_finish(dev, NULL, frozen);
2153
			break;
2154

2155
		case CPU_DOWN_FAILED:
2156
			__cpufreq_add_dev(dev, NULL, frozen);
2157 2158 2159 2160 2161 2162
			break;
		}
	}
	return NOTIFY_OK;
}

2163
static struct notifier_block __refdata cpufreq_cpu_notifier = {
2164
	.notifier_call = cpufreq_cpu_callback,
2165
};
L
Linus Torvalds 已提交
2166 2167 2168 2169 2170 2171 2172 2173 2174 2175

/*********************************************************************
 *               REGISTER / UNREGISTER CPUFREQ DRIVER                *
 *********************************************************************/

/**
 * cpufreq_register_driver - register a CPU Frequency driver
 * @driver_data: A struct cpufreq_driver containing the values#
 * submitted by the CPU Frequency driver.
 *
2176
 * Registers a CPU Frequency driver to this core code. This code
L
Linus Torvalds 已提交
2177
 * returns zero on success, -EBUSY when another driver got here first
2178
 * (and isn't unregistered in the meantime).
L
Linus Torvalds 已提交
2179 2180
 *
 */
2181
int cpufreq_register_driver(struct cpufreq_driver *driver_data)
L
Linus Torvalds 已提交
2182 2183 2184 2185
{
	unsigned long flags;
	int ret;

2186 2187 2188
	if (cpufreq_disabled())
		return -ENODEV;

L
Linus Torvalds 已提交
2189
	if (!driver_data || !driver_data->verify || !driver_data->init ||
2190 2191
	    !(driver_data->setpolicy || driver_data->target_index ||
		    driver_data->target))
L
Linus Torvalds 已提交
2192 2193
		return -EINVAL;

2194
	pr_debug("trying to register driver %s\n", driver_data->name);
L
Linus Torvalds 已提交
2195 2196 2197 2198

	if (driver_data->setpolicy)
		driver_data->flags |= CPUFREQ_CONST_LOOPS;

2199
	write_lock_irqsave(&cpufreq_driver_lock, flags);
2200
	if (cpufreq_driver) {
2201
		write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2202
		return -EEXIST;
L
Linus Torvalds 已提交
2203
	}
2204
	cpufreq_driver = driver_data;
2205
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
L
Linus Torvalds 已提交
2206

2207
	ret = subsys_interface_register(&cpufreq_interface);
2208 2209
	if (ret)
		goto err_null_driver;
L
Linus Torvalds 已提交
2210

2211
	if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
L
Linus Torvalds 已提交
2212 2213 2214 2215
		int i;
		ret = -ENODEV;

		/* check for at least one working CPU */
2216 2217
		for (i = 0; i < nr_cpu_ids; i++)
			if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
L
Linus Torvalds 已提交
2218
				ret = 0;
2219 2220
				break;
			}
L
Linus Torvalds 已提交
2221 2222 2223

		/* if all ->init() calls failed, unregister */
		if (ret) {
2224
			pr_debug("no CPU initialized for driver %s\n",
2225
							driver_data->name);
2226
			goto err_if_unreg;
L
Linus Torvalds 已提交
2227 2228 2229
		}
	}

2230
	register_hotcpu_notifier(&cpufreq_cpu_notifier);
2231
	pr_debug("driver %s up and running\n", driver_data->name);
L
Linus Torvalds 已提交
2232

2233
	return 0;
2234 2235
err_if_unreg:
	subsys_interface_unregister(&cpufreq_interface);
2236
err_null_driver:
2237
	write_lock_irqsave(&cpufreq_driver_lock, flags);
2238
	cpufreq_driver = NULL;
2239
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
D
Dave Jones 已提交
2240
	return ret;
L
Linus Torvalds 已提交
2241 2242 2243 2244 2245 2246
}
EXPORT_SYMBOL_GPL(cpufreq_register_driver);

/**
 * cpufreq_unregister_driver - unregister the current CPUFreq driver
 *
2247
 * Unregister the current CPUFreq driver. Only call this if you have
L
Linus Torvalds 已提交
2248 2249 2250 2251
 * the right to do so, i.e. if you have succeeded in initialising before!
 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
 * currently not initialised.
 */
2252
int cpufreq_unregister_driver(struct cpufreq_driver *driver)
L
Linus Torvalds 已提交
2253 2254 2255
{
	unsigned long flags;

2256
	if (!cpufreq_driver || (driver != cpufreq_driver))
L
Linus Torvalds 已提交
2257 2258
		return -EINVAL;

2259
	pr_debug("unregistering driver %s\n", driver->name);
L
Linus Torvalds 已提交
2260

2261
	subsys_interface_unregister(&cpufreq_interface);
2262
	unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
L
Linus Torvalds 已提交
2263

2264
	down_write(&cpufreq_rwsem);
2265
	write_lock_irqsave(&cpufreq_driver_lock, flags);
2266

2267
	cpufreq_driver = NULL;
2268

2269
	write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2270
	up_write(&cpufreq_rwsem);
L
Linus Torvalds 已提交
2271 2272 2273 2274

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
2275 2276 2277

static int __init cpufreq_core_init(void)
{
2278 2279 2280
	if (cpufreq_disabled())
		return -ENODEV;

2281
	cpufreq_global_kobject = kobject_create();
2282
	BUG_ON(!cpufreq_global_kobject);
2283
	register_syscore_ops(&cpufreq_syscore_ops);
2284

2285 2286 2287
	return 0;
}
core_initcall(cpufreq_core_init);