cpufreq.c 65.4 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.
 */

Viresh Kumar's avatar
Viresh Kumar committed
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
42
43
44
45
46
47
48
49
50
51

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

static bool suitable_policy(struct cpufreq_policy *policy, bool active)
{
	return active == !policy_is_inactive(policy);
}

/* Finds Next Acive/Inactive policy */
static struct cpufreq_policy *next_policy(struct cpufreq_policy *policy,
					  bool active)
{
	do {
		/* No more policies in the list */
52
		if (list_is_last(&policy->policy_list, &cpufreq_policy_list))
53
			return NULL;
54
55

		policy = list_next_entry(policy, policy_list);
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
	} while (!suitable_policy(policy, active));

	return policy;
}

static struct cpufreq_policy *first_policy(bool active)
{
	struct cpufreq_policy *policy;

	/* No policies in the list */
	if (list_empty(&cpufreq_policy_list))
		return NULL;

	policy = list_first_entry(&cpufreq_policy_list, typeof(*policy),
				  policy_list);

	if (!suitable_policy(policy, active))
		policy = next_policy(policy, active);

	return policy;
}

/* Macros to iterate over CPU policies */
#define for_each_suitable_policy(__policy, __active)	\
	for (__policy = first_policy(__active);		\
	     __policy;					\
	     __policy = next_policy(__policy, __active))

#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)			\
90
91
	list_for_each_entry(__policy, &cpufreq_policy_list, policy_list)

92
93
94
95
96
/* 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
97
/**
Dave Jones's avatar
Dave Jones committed
98
 * The "cpufreq driver" - the arch- or hardware-dependent low
Linus Torvalds's avatar
Linus Torvalds committed
99
100
101
 * level driver of CPUFreq support, and its spinlock. This lock
 * also protects the cpufreq_cpu_data array.
 */
102
static struct cpufreq_driver *cpufreq_driver;
103
static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
104
static DEFINE_RWLOCK(cpufreq_driver_lock);
105
106
107
108
109
110
111
112
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
148
149

static DEFINE_PER_CPU(struct update_util_data *, cpufreq_update_util_data);

/**
 * cpufreq_set_update_util_data - Populate the CPU's update_util_data pointer.
 * @cpu: The CPU to set the pointer for.
 * @data: New pointer value.
 *
 * Set and publish the update_util_data pointer for the given CPU.  That pointer
 * points to a struct update_util_data object containing a callback function
 * to call from cpufreq_update_util().  That function will be called from an RCU
 * read-side critical section, so it must not sleep.
 *
 * Callers must use RCU callbacks to free any memory that might be accessed
 * via the old update_util_data pointer or invoke synchronize_rcu() right after
 * this function to avoid use-after-free.
 */
void cpufreq_set_update_util_data(int cpu, struct update_util_data *data)
{
	rcu_assign_pointer(per_cpu(cpufreq_update_util_data, cpu), data);
}
EXPORT_SYMBOL_GPL(cpufreq_set_update_util_data);

/**
 * cpufreq_update_util - Take a note about CPU utilization changes.
 * @time: Current time.
 * @util: Current utilization.
 * @max: Utilization ceiling.
 *
 * This function is called by the scheduler on every invocation of
 * update_load_avg() on the CPU whose utilization is being updated.
 */
void cpufreq_update_util(u64 time, unsigned long util, unsigned long max)
{
	struct update_util_data *data;

	rcu_read_lock();

	data = rcu_dereference(*this_cpu_ptr(&cpufreq_update_util_data));
	if (data && data->func)
		data->func(data, time, util, max);

	rcu_read_unlock();
}

150
DEFINE_MUTEX(cpufreq_governor_lock);
151

152
153
/* Flag to suspend/resume CPUFreq governors */
static bool cpufreq_suspended;
Linus Torvalds's avatar
Linus Torvalds committed
154

155
156
157
158
159
static inline bool has_target(void)
{
	return cpufreq_driver->target_index || cpufreq_driver->target;
}

Linus Torvalds's avatar
Linus Torvalds committed
160
/* internal prototypes */
161
162
static int __cpufreq_governor(struct cpufreq_policy *policy,
		unsigned int event);
163
static unsigned int __cpufreq_get(struct cpufreq_policy *policy);
164
static void handle_update(struct work_struct *work);
Linus Torvalds's avatar
Linus Torvalds committed
165
166

/**
167
168
 * 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
169
170
171
172
 * "transition" list for kernel code that needs to handle
 * changes to devices when the CPU clock speed changes.
 * The mutex locks both lists.
 */
173
static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
174
static struct srcu_notifier_head cpufreq_transition_notifier_list;
Linus Torvalds's avatar
Linus Torvalds committed
175

176
static bool init_cpufreq_transition_notifier_list_called;
177
178
179
static int __init init_cpufreq_transition_notifier_list(void)
{
	srcu_init_notifier_head(&cpufreq_transition_notifier_list);
180
	init_cpufreq_transition_notifier_list_called = true;
181
182
	return 0;
}
183
pure_initcall(init_cpufreq_transition_notifier_list);
Linus Torvalds's avatar
Linus Torvalds committed
184

185
static int off __read_mostly;
186
static int cpufreq_disabled(void)
187
188
189
190
191
192
193
{
	return off;
}
void disable_cpufreq(void)
{
	off = 1;
}
194
static DEFINE_MUTEX(cpufreq_governor_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
195

196
197
bool have_governor_per_policy(void)
{
198
	return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
199
}
200
EXPORT_SYMBOL_GPL(have_governor_per_policy);
201

202
203
204
205
206
207
208
209
210
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);

211
212
213
214
215
216
217
218
219
struct cpufreq_frequency_table *cpufreq_frequency_get_table(unsigned int cpu)
{
	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);

	return policy && !policy_is_inactive(policy) ?
		policy->freq_table : NULL;
}
EXPORT_SYMBOL_GPL(cpufreq_frequency_get_table);

220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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);

255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * 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;

	/*
277
	 * The driver only supports the SMP configuration where all processors
278
279
280
281
282
283
284
285
	 * share the clock and voltage and clock.
	 */
	cpumask_setall(policy->cpus);

	return 0;
}
EXPORT_SYMBOL_GPL(cpufreq_generic_init);

286
struct cpufreq_policy *cpufreq_cpu_get_raw(unsigned int cpu)
287
288
289
{
	struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);

290
291
	return policy && cpumask_test_cpu(cpu, policy->cpus) ? policy : NULL;
}
292
EXPORT_SYMBOL_GPL(cpufreq_cpu_get_raw);
293
294
295
296
297

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

298
	if (!policy || IS_ERR(policy->clk)) {
299
300
		pr_err("%s: No %s associated to cpu: %d\n",
		       __func__, policy ? "clk" : "policy", cpu);
301
302
303
304
305
306
307
		return 0;
	}

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

308
309
310
311
312
313
314
315
316
317
318
319
320
/**
 * 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.
 */
321
struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
Linus Torvalds's avatar
Linus Torvalds committed
322
{
323
	struct cpufreq_policy *policy = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
324
325
	unsigned long flags;

326
	if (WARN_ON(cpu >= nr_cpu_ids))
327
328
		return NULL;

Linus Torvalds's avatar
Linus Torvalds committed
329
	/* get the cpufreq driver */
330
	read_lock_irqsave(&cpufreq_driver_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
331

332
333
	if (cpufreq_driver) {
		/* get the CPU */
334
		policy = cpufreq_cpu_get_raw(cpu);
335
336
337
		if (policy)
			kobject_get(&policy->kobj);
	}
Linus Torvalds's avatar
Linus Torvalds committed
338

339
	read_unlock_irqrestore(&cpufreq_driver_lock, flags);
Linus Torvalds's avatar
Linus Torvalds committed
340

341
	return policy;
342
}
Linus Torvalds's avatar
Linus Torvalds committed
343
344
EXPORT_SYMBOL_GPL(cpufreq_cpu_get);

345
346
347
348
349
350
351
352
/**
 * 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().
 */
353
void cpufreq_cpu_put(struct cpufreq_policy *policy)
Linus Torvalds's avatar
Linus Torvalds committed
354
{
355
	kobject_put(&policy->kobj);
Linus Torvalds's avatar
Linus Torvalds committed
356
357
358
359
360
361
362
363
364
365
366
367
}
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
368
 * systems as each CPU might be scaled differently. So, use the arch
Linus Torvalds's avatar
Linus Torvalds committed
369
370
 * per-CPU loops_per_jiffy value wherever possible.
 */
371
static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
Linus Torvalds's avatar
Linus Torvalds committed
372
{
373
374
375
376
#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
377
378
379
380
381
382
	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;
383
384
		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
385
	}
386
	if (val == CPUFREQ_POSTCHANGE && ci->old != ci->new) {
387
388
		loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
								ci->new);
389
390
		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
391
392
	}
#endif
393
}
Linus Torvalds's avatar
Linus Torvalds committed
394

395
static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
396
		struct cpufreq_freqs *freqs, unsigned int state)
Linus Torvalds's avatar
Linus Torvalds committed
397
398
399
{
	BUG_ON(irqs_disabled());

400
401
402
	if (cpufreq_disabled())
		return;

403
	freqs->flags = cpufreq_driver->flags;
404
	pr_debug("notification %u of frequency transition to %u kHz\n",
405
		 state, freqs->new);
Linus Torvalds's avatar
Linus Torvalds committed
406
407

	switch (state) {
408

Linus Torvalds's avatar
Linus Torvalds committed
409
	case CPUFREQ_PRECHANGE:
410
		/* detect if the driver reported a value as "old frequency"
411
412
		 * which is not equal to what the cpufreq core thinks is
		 * "old frequency".
Linus Torvalds's avatar
Linus Torvalds committed
413
		 */
414
		if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
415
416
			if ((policy) && (policy->cpu == freqs->cpu) &&
			    (policy->cur) && (policy->cur != freqs->old)) {
417
418
				pr_debug("Warning: CPU frequency is %u, cpufreq assumed %u kHz\n",
					 freqs->old, policy->cur);
419
				freqs->old = policy->cur;
Linus Torvalds's avatar
Linus Torvalds committed
420
421
			}
		}
422
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
423
				CPUFREQ_PRECHANGE, freqs);
Linus Torvalds's avatar
Linus Torvalds committed
424
425
		adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
		break;
426

Linus Torvalds's avatar
Linus Torvalds committed
427
428
	case CPUFREQ_POSTCHANGE:
		adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
429
430
		pr_debug("FREQ: %lu - CPU: %lu\n",
			 (unsigned long)freqs->new, (unsigned long)freqs->cpu);
431
		trace_cpu_frequency(freqs->new, freqs->cpu);
432
		srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
433
				CPUFREQ_POSTCHANGE, freqs);
434
435
		if (likely(policy) && likely(policy->cpu == freqs->cpu))
			policy->cur = freqs->new;
Linus Torvalds's avatar
Linus Torvalds committed
436
437
438
		break;
	}
}
439

440
441
442
443
444
445
446
447
/**
 * 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.
 */
448
static void cpufreq_notify_transition(struct cpufreq_policy *policy,
449
450
451
452
453
		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
454

455
/* Do post notifications when there are chances that transition has failed */
456
static void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
457
458
459
460
461
462
463
464
465
466
467
		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);
}

468
469
470
void cpufreq_freq_transition_begin(struct cpufreq_policy *policy,
		struct cpufreq_freqs *freqs)
{
471
472
473
474
475
476
477
478
479
480
481
482

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

483
484
485
486
487
488
489
490
491
492
493
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;
494
	policy->transition_task = current;
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

	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;
511
	policy->transition_task = NULL;
512
513
514
515
516

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

Linus Torvalds's avatar
Linus Torvalds committed
517
518
519
520

/*********************************************************************
 *                          SYSFS INTERFACE                          *
 *********************************************************************/
521
static ssize_t show_boost(struct kobject *kobj,
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
				 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)) {
537
538
		pr_err("%s: Cannot %s BOOST!\n",
		       __func__, enable ? "enable" : "disable");
539
540
541
		return -EINVAL;
	}

542
543
	pr_debug("%s: cpufreq BOOST %s\n",
		 __func__, enable ? "enabled" : "disabled");
544
545
546
547

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

549
static struct cpufreq_governor *find_governor(const char *str_governor)
550
551
552
{
	struct cpufreq_governor *t;

553
	for_each_governor(t)
554
		if (!strncasecmp(str_governor, t->name, CPUFREQ_NAME_LEN))
555
556
557
558
559
			return t;

	return NULL;
}

Linus Torvalds's avatar
Linus Torvalds committed
560
561
562
/**
 * cpufreq_parse_governor - parse a governor string
 */
Dave Jones's avatar
Dave Jones committed
563
static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
Linus Torvalds's avatar
Linus Torvalds committed
564
565
				struct cpufreq_governor **governor)
{
566
	int err = -EINVAL;
567
568

	if (cpufreq_driver->setpolicy) {
569
		if (!strncasecmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
Linus Torvalds's avatar
Linus Torvalds committed
570
			*policy = CPUFREQ_POLICY_PERFORMANCE;
571
			err = 0;
572
		} else if (!strncasecmp(str_governor, "powersave",
573
						CPUFREQ_NAME_LEN)) {
Linus Torvalds's avatar
Linus Torvalds committed
574
			*policy = CPUFREQ_POLICY_POWERSAVE;
575
			err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
576
		}
577
	} else {
Linus Torvalds's avatar
Linus Torvalds committed
578
		struct cpufreq_governor *t;
579

580
		mutex_lock(&cpufreq_governor_mutex);
581

582
		t = find_governor(str_governor);
583

584
		if (t == NULL) {
585
			int ret;
586

587
588
589
			mutex_unlock(&cpufreq_governor_mutex);
			ret = request_module("cpufreq_%s", str_governor);
			mutex_lock(&cpufreq_governor_mutex);
590

591
			if (ret == 0)
592
				t = find_governor(str_governor);
593
594
		}

595
596
597
		if (t != NULL) {
			*governor = t;
			err = 0;
Linus Torvalds's avatar
Linus Torvalds committed
598
		}
599

600
		mutex_unlock(&cpufreq_governor_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
601
	}
602
	return err;
Linus Torvalds's avatar
Linus Torvalds committed
603
604
605
}

/**
606
607
 * cpufreq_per_cpu_attr_read() / show_##file_name() -
 * print out cpufreq information
Linus Torvalds's avatar
Linus Torvalds committed
608
609
610
611
612
 *
 * Write out information from cpufreq_driver->policy[cpu]; object must be
 * "unsigned int".
 */

613
614
#define show_one(file_name, object)			\
static ssize_t show_##file_name				\
Dave Jones's avatar
Dave Jones committed
615
(struct cpufreq_policy *policy, char *buf)		\
616
{							\
617
	return sprintf(buf, "%u\n", policy->object);	\
Linus Torvalds's avatar
Linus Torvalds committed
618
619
620
621
}

show_one(cpuinfo_min_freq, cpuinfo.min_freq);
show_one(cpuinfo_max_freq, cpuinfo.max_freq);
622
show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
Linus Torvalds's avatar
Linus Torvalds committed
623
624
show_one(scaling_min_freq, min);
show_one(scaling_max_freq, max);
625

626
static ssize_t show_scaling_cur_freq(struct cpufreq_policy *policy, char *buf)
627
628
629
630
631
632
633
634
635
{
	ssize_t ret;

	if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
		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
636

637
static int cpufreq_set_policy(struct cpufreq_policy *policy,
638
				struct cpufreq_policy *new_policy);
639

Linus Torvalds's avatar
Linus Torvalds committed
640
641
642
643
644
/**
 * 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
645
(struct cpufreq_policy *policy, const char *buf, size_t count)		\
Linus Torvalds's avatar
Linus Torvalds committed
646
{									\
647
	int ret, temp;							\
Linus Torvalds's avatar
Linus Torvalds committed
648
649
	struct cpufreq_policy new_policy;				\
									\
650
	memcpy(&new_policy, policy, sizeof(*policy));			\
Linus Torvalds's avatar
Linus Torvalds committed
651
									\
652
	ret = sscanf(buf, "%u", &new_policy.object);			\
Linus Torvalds's avatar
Linus Torvalds committed
653
654
655
	if (ret != 1)							\
		return -EINVAL;						\
									\
656
	temp = new_policy.object;					\
657
	ret = cpufreq_set_policy(policy, &new_policy);		\
658
659
	if (!ret)							\
		policy->user_policy.object = temp;			\
Linus Torvalds's avatar
Linus Torvalds committed
660
661
662
663
									\
	return ret ? ret : count;					\
}

664
665
store_one(scaling_min_freq, min);
store_one(scaling_max_freq, max);
Linus Torvalds's avatar
Linus Torvalds committed
666
667
668
669

/**
 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
 */
Dave Jones's avatar
Dave Jones committed
670
671
static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
					char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
672
{
673
	unsigned int cur_freq = __cpufreq_get(policy);
Linus Torvalds's avatar
Linus Torvalds committed
674
675
676
677
678
679
680
681
	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
 */
Dave Jones's avatar
Dave Jones committed
682
static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
683
{
684
	if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
Linus Torvalds's avatar
Linus Torvalds committed
685
686
687
688
		return sprintf(buf, "powersave\n");
	else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
		return sprintf(buf, "performance\n");
	else if (policy->governor)
689
		return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
690
				policy->governor->name);
Linus Torvalds's avatar
Linus Torvalds committed
691
692
693
694
695
696
	return -EINVAL;
}

/**
 * store_scaling_governor - store policy for the specified CPU
 */
Dave Jones's avatar
Dave Jones committed
697
698
static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
					const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
699
{
700
	int ret;
Linus Torvalds's avatar
Linus Torvalds committed
701
702
703
	char	str_governor[16];
	struct cpufreq_policy new_policy;

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

706
	ret = sscanf(buf, "%15s", str_governor);
Linus Torvalds's avatar
Linus Torvalds committed
707
708
709
	if (ret != 1)
		return -EINVAL;

710
711
	if (cpufreq_parse_governor(str_governor, &new_policy.policy,
						&new_policy.governor))
Linus Torvalds's avatar
Linus Torvalds committed
712
713
		return -EINVAL;

714
	ret = cpufreq_set_policy(policy, &new_policy);
715
	return ret ? ret : count;
Linus Torvalds's avatar
Linus Torvalds committed
716
717
718
719
720
}

/**
 * show_scaling_driver - show the cpufreq driver currently loaded
 */
Dave Jones's avatar
Dave Jones committed
721
static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
722
{
723
	return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
Linus Torvalds's avatar
Linus Torvalds committed
724
725
726
727
728
}

/**
 * show_scaling_available_governors - show the available CPUfreq governors
 */
Dave Jones's avatar
Dave Jones committed
729
730
static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
						char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
731
732
733
734
{
	ssize_t i = 0;
	struct cpufreq_governor *t;

735
	if (!has_target()) {
Linus Torvalds's avatar
Linus Torvalds committed
736
737
738
739
		i += sprintf(buf, "performance powersave");
		goto out;
	}

740
	for_each_governor(t) {
741
742
		if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
		    - (CPUFREQ_NAME_LEN + 2)))
Linus Torvalds's avatar
Linus Torvalds committed
743
			goto out;
744
		i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
Linus Torvalds's avatar
Linus Torvalds committed
745
	}
746
out:
Linus Torvalds's avatar
Linus Torvalds committed
747
748
749
	i += sprintf(&buf[i], "\n");
	return i;
}
750

751
ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
752
753
754
755
{
	ssize_t i = 0;
	unsigned int cpu;

756
	for_each_cpu(cpu, mask) {
Linus Torvalds's avatar
Linus Torvalds committed
757
758
759
760
		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))
761
			break;
Linus Torvalds's avatar
Linus Torvalds committed
762
763
764
765
	}
	i += sprintf(&buf[i], "\n");
	return i;
}
766
EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
Linus Torvalds's avatar
Linus Torvalds committed
767

768
769
770
771
772
773
/**
 * 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)
{
774
	return cpufreq_show_cpus(policy->related_cpus, buf);
775
776
777
778
779
780
781
}

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

785
static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
Dave Jones's avatar
Dave Jones committed
786
					const char *buf, size_t count)
787
788
789
790
{
	unsigned int freq = 0;
	unsigned int ret;

791
	if (!policy->governor || !policy->governor->store_setspeed)
792
793
794
795
796
797
798
799
800
801
802
803
804
		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)
{
805
	if (!policy->governor || !policy->governor->show_setspeed)
806
807
808
809
		return sprintf(buf, "<unsupported>\n");

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

811
/**
812
 * show_bios_limit - show the current cpufreq HW/BIOS limitation
813
814
815
816
817
 */
static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
{
	unsigned int limit;
	int ret;
818
819
	if (cpufreq_driver->bios_limit) {
		ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
820
821
822
823
824
825
		if (!ret)
			return sprintf(buf, "%u\n", limit);
	}
	return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
}

826
827
828
829
830
831
832
833
834
835
836
837
838
839
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
840

Dave Jones's avatar
Dave Jones committed
841
static struct attribute *default_attrs[] = {
Linus Torvalds's avatar
Linus Torvalds committed
842
843
	&cpuinfo_min_freq.attr,
	&cpuinfo_max_freq.attr,
844
	&cpuinfo_transition_latency.attr,
Linus Torvalds's avatar
Linus Torvalds committed
845
846
847
	&scaling_min_freq.attr,
	&scaling_max_freq.attr,
	&affected_cpus.attr,
848
	&related_cpus.attr,
Linus Torvalds's avatar
Linus Torvalds committed
849
850
851
	&scaling_governor.attr,
	&scaling_driver.attr,
	&scaling_available_governors.attr,
852
	&scaling_setspeed.attr,
Linus Torvalds's avatar
Linus Torvalds committed
853
854
855
	NULL
};

856
857
#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
858

859
static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
860
{
Dave Jones's avatar
Dave Jones committed
861
862
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
863
	ssize_t ret;
864

865
	down_read(&policy->rwsem);
866

867
868
869
870
871
	if (fattr->show)
		ret = fattr->show(policy, buf);
	else
		ret = -EIO;

872
	up_read(&policy->rwsem);
873

Linus Torvalds's avatar
Linus Torvalds committed
874
875
876
	return ret;
}

Dave Jones's avatar
Dave Jones committed
877
878
static ssize_t store(struct kobject *kobj, struct attribute *attr,
		     const char *buf, size_t count)
Linus Torvalds's avatar
Linus Torvalds committed
879
{
Dave Jones's avatar
Dave Jones committed
880
881
	struct cpufreq_policy *policy = to_policy(kobj);
	struct freq_attr *fattr = to_attr(attr);
882
	ssize_t ret = -EINVAL;
883

884
885
886
887
888
	get_online_cpus();

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

889
	down_write(&policy->rwsem);
890

891
892
893
894
895
	if (fattr->store)
		ret = fattr->store(policy, buf, count);
	else
		ret = -EIO;

896
	up_write(&policy->rwsem);
897
898
899
unlock:
	put_online_cpus();

Linus Torvalds's avatar
Linus Torvalds committed
900
901
902
	return ret;
}

Dave Jones's avatar
Dave Jones committed
903
static void cpufreq_sysfs_release(struct kobject *kobj)
Linus Torvalds's avatar
Linus Torvalds committed
904
{
Dave Jones's avatar
Dave Jones committed
905
	struct cpufreq_policy *policy = to_policy(kobj);
906
	pr_debug("last reference is dropped\n");
Linus Torvalds's avatar
Linus Torvalds committed
907
908
909
	complete(&policy->kobj_unregister);
}

910
static const struct sysfs_ops sysfs_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
911
912
913
914
915
916
917
918
919
920
	.show	= show,
	.store	= store,
};

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

921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
static int add_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
{
	struct device *cpu_dev;

	pr_debug("%s: Adding symlink for CPU: %u\n", __func__, cpu);

	if (!policy)
		return 0;

	cpu_dev = get_cpu_device(cpu);
	if (WARN_ON(!cpu_dev))
		return 0;

	return sysfs_create_link(&cpu_dev->kobj, &policy->kobj, "cpufreq");
}

static void remove_cpu_dev_symlink(struct cpufreq_policy *policy, int cpu)
{
	struct device *cpu_dev;

	pr_debug("%s: Removing symlink for CPU: %u\n", __func__, cpu);

	cpu_dev = get_cpu_device(cpu);
	if (WARN_ON(!cpu_dev))
		return;

	sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
}

/* Add/remove symlinks for all related CPUs */
951
static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
952
953
954
955
{
	unsigned int j;
	int ret = 0;

956
	/* Some related CPUs might not be present (physically hotplugged) */
957
	for_each_cpu(j, policy->real_cpus) {
958
		ret = add_cpu_dev_symlink(policy, j);
959
960
		if (ret)
			break;
961
	}
962

963
964
965
	return ret;
}

966
967
968
969
970
static void cpufreq_remove_dev_symlink(struct cpufreq_policy *policy)
{
	unsigned int j;

	/* Some related CPUs might not be present (physically hotplugged) */
971
	for_each_cpu(j, policy->real_cpus)
972
973
974
		remove_cpu_dev_symlink(policy, j);
}

975
static int cpufreq_add_dev_interface(struct cpufreq_policy *policy)
976
977
978
979
980
{
	struct freq_attr **drv_attr;
	int ret = 0;

	/* set up files for this cpu device */
981
	drv_attr = cpufreq_driver->attr;
982
	while (drv_attr && *drv_attr) {
983
984
		ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
		if (ret)
985
			return ret;
986
987
		drv_attr++;
	}
988
	if (cpufreq_driver->get) {
989
990
		ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
		if (ret)
991
			return ret;
992
	}
993
994
995

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

998
	if (cpufreq_driver->bios_limit) {
999
1000
		ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
		if (ret)
For faster browsing, not all history is shown. View entire blame