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