intel_pstate: Add Baytrail support
[deliverable/linux.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>
bb176f7d 6 * (C) 2013 Viresh Kumar <viresh.kumar@linaro.org>
1da177e4 7 *
c32b6b8e 8 * Oct 2005 - Ashok Raj <ashok.raj@intel.com>
32ee8c3e 9 * Added handling for CPU hotplug
8ff69732
DJ
10 * Feb 2006 - Jacob Shin <jacob.shin@amd.com>
11 * Fix handling for CPU hotplug -- affected CPUs
c32b6b8e 12 *
1da177e4
LT
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
1da177e4
LT
16 */
17
db701151
VK
18#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19
5ff0a268 20#include <linux/cpu.h>
1da177e4
LT
21#include <linux/cpufreq.h>
22#include <linux/delay.h>
1da177e4 23#include <linux/device.h>
5ff0a268
VK
24#include <linux/init.h>
25#include <linux/kernel_stat.h>
26#include <linux/module.h>
3fc54d37 27#include <linux/mutex.h>
5ff0a268 28#include <linux/slab.h>
e00e56df 29#include <linux/syscore_ops.h>
5ff0a268 30#include <linux/tick.h>
6f4f2723
TR
31#include <trace/events/power.h>
32
1da177e4 33/**
cd878479 34 * The "cpufreq driver" - the arch- or hardware-dependent low
1da177e4
LT
35 * level driver of CPUFreq support, and its spinlock. This lock
36 * also protects the cpufreq_cpu_data array.
37 */
1c3d85dd 38static struct cpufreq_driver *cpufreq_driver;
7a6aedfa 39static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
8414809c 40static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
bb176f7d
VK
41static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock);
c88a1f8b 43static LIST_HEAD(cpufreq_policy_list);
bb176f7d 44
084f3493
TR
45#ifdef CONFIG_HOTPLUG_CPU
46/* This one keeps track of the previously set governor of a removed CPU */
e77b89f1 47static DEFINE_PER_CPU(char[CPUFREQ_NAME_LEN], cpufreq_cpu_governor);
084f3493 48#endif
1da177e4 49
9c0ebcf7
VK
50static inline bool has_target(void)
51{
52 return cpufreq_driver->target_index || cpufreq_driver->target;
53}
54
5a01f2e8
VP
55/*
56 * cpu_policy_rwsem is a per CPU reader-writer semaphore designed to cure
57 * all cpufreq/hotplug/workqueue/etc related lock issues.
58 *
59 * The rules for this semaphore:
60 * - Any routine that wants to read from the policy structure will
61 * do a down_read on this semaphore.
62 * - Any routine that will write to the policy structure and/or may take away
63 * the policy altogether (eg. CPU hotplug), will hold this lock in write
64 * mode before doing so.
65 *
66 * Additional rules:
5a01f2e8
VP
67 * - Governor routines that can be called in cpufreq hotplug path should not
68 * take this sem as top level hotplug notifier handler takes this.
395913d0
MD
69 * - Lock should not be held across
70 * __cpufreq_governor(data, CPUFREQ_GOV_STOP);
5a01f2e8 71 */
5a01f2e8
VP
72static DEFINE_PER_CPU(struct rw_semaphore, cpu_policy_rwsem);
73
74#define lock_policy_rwsem(mode, cpu) \
1b750e3b 75static void lock_policy_rwsem_##mode(int cpu) \
5a01f2e8 76{ \
474deff7
VK
77 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
78 BUG_ON(!policy); \
79 down_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8
VP
80}
81
82lock_policy_rwsem(read, cpu);
5a01f2e8 83lock_policy_rwsem(write, cpu);
5a01f2e8 84
fa1d8af4
VK
85#define unlock_policy_rwsem(mode, cpu) \
86static void unlock_policy_rwsem_##mode(int cpu) \
87{ \
474deff7
VK
88 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); \
89 BUG_ON(!policy); \
90 up_##mode(&per_cpu(cpu_policy_rwsem, policy->cpu)); \
5a01f2e8 91}
5a01f2e8 92
fa1d8af4
VK
93unlock_policy_rwsem(read, cpu);
94unlock_policy_rwsem(write, cpu);
5a01f2e8 95
6eed9404
VK
96/*
97 * rwsem to guarantee that cpufreq driver module doesn't unload during critical
98 * sections
99 */
100static DECLARE_RWSEM(cpufreq_rwsem);
101
1da177e4 102/* internal prototypes */
29464f28
DJ
103static int __cpufreq_governor(struct cpufreq_policy *policy,
104 unsigned int event);
5a01f2e8 105static unsigned int __cpufreq_get(unsigned int cpu);
65f27f38 106static void handle_update(struct work_struct *work);
1da177e4
LT
107
108/**
32ee8c3e
DJ
109 * Two notifier lists: the "policy" list is involved in the
110 * validation process for a new CPU frequency policy; the
1da177e4
LT
111 * "transition" list for kernel code that needs to handle
112 * changes to devices when the CPU clock speed changes.
113 * The mutex locks both lists.
114 */
e041c683 115static BLOCKING_NOTIFIER_HEAD(cpufreq_policy_notifier_list);
b4dfdbb3 116static struct srcu_notifier_head cpufreq_transition_notifier_list;
1da177e4 117
74212ca4 118static bool init_cpufreq_transition_notifier_list_called;
b4dfdbb3
AS
119static int __init init_cpufreq_transition_notifier_list(void)
120{
121 srcu_init_notifier_head(&cpufreq_transition_notifier_list);
74212ca4 122 init_cpufreq_transition_notifier_list_called = true;
b4dfdbb3
AS
123 return 0;
124}
b3438f82 125pure_initcall(init_cpufreq_transition_notifier_list);
1da177e4 126
a7b422cd 127static int off __read_mostly;
da584455 128static int cpufreq_disabled(void)
a7b422cd
KRW
129{
130 return off;
131}
132void disable_cpufreq(void)
133{
134 off = 1;
135}
1da177e4 136static LIST_HEAD(cpufreq_governor_list);
29464f28 137static DEFINE_MUTEX(cpufreq_governor_mutex);
1da177e4 138
4d5dcc42
VK
139bool have_governor_per_policy(void)
140{
0b981e70 141 return !!(cpufreq_driver->flags & CPUFREQ_HAVE_GOVERNOR_PER_POLICY);
4d5dcc42 142}
3f869d6d 143EXPORT_SYMBOL_GPL(have_governor_per_policy);
4d5dcc42 144
944e9a03
VK
145struct kobject *get_governor_parent_kobj(struct cpufreq_policy *policy)
146{
147 if (have_governor_per_policy())
148 return &policy->kobj;
149 else
150 return cpufreq_global_kobject;
151}
152EXPORT_SYMBOL_GPL(get_governor_parent_kobj);
153
72a4ce34
VK
154static inline u64 get_cpu_idle_time_jiffy(unsigned int cpu, u64 *wall)
155{
156 u64 idle_time;
157 u64 cur_wall_time;
158 u64 busy_time;
159
160 cur_wall_time = jiffies64_to_cputime64(get_jiffies_64());
161
162 busy_time = kcpustat_cpu(cpu).cpustat[CPUTIME_USER];
163 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SYSTEM];
164 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_IRQ];
165 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_SOFTIRQ];
166 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_STEAL];
167 busy_time += kcpustat_cpu(cpu).cpustat[CPUTIME_NICE];
168
169 idle_time = cur_wall_time - busy_time;
170 if (wall)
171 *wall = cputime_to_usecs(cur_wall_time);
172
173 return cputime_to_usecs(idle_time);
174}
175
176u64 get_cpu_idle_time(unsigned int cpu, u64 *wall, int io_busy)
177{
178 u64 idle_time = get_cpu_idle_time_us(cpu, io_busy ? wall : NULL);
179
180 if (idle_time == -1ULL)
181 return get_cpu_idle_time_jiffy(cpu, wall);
182 else if (!io_busy)
183 idle_time += get_cpu_iowait_time_us(cpu, wall);
184
185 return idle_time;
186}
187EXPORT_SYMBOL_GPL(get_cpu_idle_time);
188
70e9e778
VK
189/*
190 * This is a generic cpufreq init() routine which can be used by cpufreq
191 * drivers of SMP systems. It will do following:
192 * - validate & show freq table passed
193 * - set policies transition latency
194 * - policy->cpus with all possible CPUs
195 */
196int cpufreq_generic_init(struct cpufreq_policy *policy,
197 struct cpufreq_frequency_table *table,
198 unsigned int transition_latency)
199{
200 int ret;
201
202 ret = cpufreq_table_validate_and_show(policy, table);
203 if (ret) {
204 pr_err("%s: invalid frequency table: %d\n", __func__, ret);
205 return ret;
206 }
207
208 policy->cpuinfo.transition_latency = transition_latency;
209
210 /*
211 * The driver only supports the SMP configuartion where all processors
212 * share the clock and voltage and clock.
213 */
214 cpumask_setall(policy->cpus);
215
216 return 0;
217}
218EXPORT_SYMBOL_GPL(cpufreq_generic_init);
219
6eed9404 220struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
1da177e4 221{
6eed9404 222 struct cpufreq_policy *policy = NULL;
1da177e4
LT
223 unsigned long flags;
224
6eed9404
VK
225 if (cpufreq_disabled() || (cpu >= nr_cpu_ids))
226 return NULL;
227
228 if (!down_read_trylock(&cpufreq_rwsem))
229 return NULL;
1da177e4
LT
230
231 /* get the cpufreq driver */
1c3d85dd 232 read_lock_irqsave(&cpufreq_driver_lock, flags);
1da177e4 233
6eed9404
VK
234 if (cpufreq_driver) {
235 /* get the CPU */
236 policy = per_cpu(cpufreq_cpu_data, cpu);
237 if (policy)
238 kobject_get(&policy->kobj);
239 }
1da177e4 240
6eed9404 241 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 242
3a3e9e06 243 if (!policy)
6eed9404 244 up_read(&cpufreq_rwsem);
1da177e4 245
3a3e9e06 246 return policy;
a9144436 247}
1da177e4
LT
248EXPORT_SYMBOL_GPL(cpufreq_cpu_get);
249
3a3e9e06 250void cpufreq_cpu_put(struct cpufreq_policy *policy)
1da177e4 251{
d5aaffa9
DB
252 if (cpufreq_disabled())
253 return;
254
6eed9404
VK
255 kobject_put(&policy->kobj);
256 up_read(&cpufreq_rwsem);
1da177e4
LT
257}
258EXPORT_SYMBOL_GPL(cpufreq_cpu_put);
259
1da177e4
LT
260/*********************************************************************
261 * EXTERNALLY AFFECTING FREQUENCY CHANGES *
262 *********************************************************************/
263
264/**
265 * adjust_jiffies - adjust the system "loops_per_jiffy"
266 *
267 * This function alters the system "loops_per_jiffy" for the clock
268 * speed change. Note that loops_per_jiffy cannot be updated on SMP
32ee8c3e 269 * systems as each CPU might be scaled differently. So, use the arch
1da177e4
LT
270 * per-CPU loops_per_jiffy value wherever possible.
271 */
272#ifndef CONFIG_SMP
273static unsigned long l_p_j_ref;
bb176f7d 274static unsigned int l_p_j_ref_freq;
1da177e4 275
858119e1 276static void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
1da177e4
LT
277{
278 if (ci->flags & CPUFREQ_CONST_LOOPS)
279 return;
280
281 if (!l_p_j_ref_freq) {
282 l_p_j_ref = loops_per_jiffy;
283 l_p_j_ref_freq = ci->old;
2d06d8c4 284 pr_debug("saving %lu as reference value for loops_per_jiffy; "
e08f5f5b 285 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
1da177e4 286 }
bb176f7d 287 if ((val == CPUFREQ_POSTCHANGE && ci->old != ci->new) ||
42d4dc3f 288 (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE)) {
e08f5f5b
GS
289 loops_per_jiffy = cpufreq_scale(l_p_j_ref, l_p_j_ref_freq,
290 ci->new);
2d06d8c4 291 pr_debug("scaling loops_per_jiffy to %lu "
e08f5f5b 292 "for frequency %u kHz\n", loops_per_jiffy, ci->new);
1da177e4
LT
293 }
294}
295#else
e08f5f5b
GS
296static inline void adjust_jiffies(unsigned long val, struct cpufreq_freqs *ci)
297{
298 return;
299}
1da177e4
LT
300#endif
301
0956df9c 302static void __cpufreq_notify_transition(struct cpufreq_policy *policy,
b43a7ffb 303 struct cpufreq_freqs *freqs, unsigned int state)
1da177e4
LT
304{
305 BUG_ON(irqs_disabled());
306
d5aaffa9
DB
307 if (cpufreq_disabled())
308 return;
309
1c3d85dd 310 freqs->flags = cpufreq_driver->flags;
2d06d8c4 311 pr_debug("notification %u of frequency transition to %u kHz\n",
e4472cb3 312 state, freqs->new);
1da177e4 313
1da177e4 314 switch (state) {
e4472cb3 315
1da177e4 316 case CPUFREQ_PRECHANGE:
32ee8c3e 317 /* detect if the driver reported a value as "old frequency"
e4472cb3
DJ
318 * which is not equal to what the cpufreq core thinks is
319 * "old frequency".
1da177e4 320 */
1c3d85dd 321 if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e4472cb3
DJ
322 if ((policy) && (policy->cpu == freqs->cpu) &&
323 (policy->cur) && (policy->cur != freqs->old)) {
2d06d8c4 324 pr_debug("Warning: CPU frequency is"
e4472cb3
DJ
325 " %u, cpufreq assumed %u kHz.\n",
326 freqs->old, policy->cur);
327 freqs->old = policy->cur;
1da177e4
LT
328 }
329 }
b4dfdbb3 330 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 331 CPUFREQ_PRECHANGE, freqs);
1da177e4
LT
332 adjust_jiffies(CPUFREQ_PRECHANGE, freqs);
333 break;
e4472cb3 334
1da177e4
LT
335 case CPUFREQ_POSTCHANGE:
336 adjust_jiffies(CPUFREQ_POSTCHANGE, freqs);
2d06d8c4 337 pr_debug("FREQ: %lu - CPU: %lu", (unsigned long)freqs->new,
6f4f2723 338 (unsigned long)freqs->cpu);
25e41933 339 trace_cpu_frequency(freqs->new, freqs->cpu);
b4dfdbb3 340 srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
e041c683 341 CPUFREQ_POSTCHANGE, freqs);
e4472cb3
DJ
342 if (likely(policy) && likely(policy->cpu == freqs->cpu))
343 policy->cur = freqs->new;
1da177e4
LT
344 break;
345 }
1da177e4 346}
bb176f7d 347
b43a7ffb
VK
348/**
349 * cpufreq_notify_transition - call notifier chain and adjust_jiffies
350 * on frequency transition.
351 *
352 * This function calls the transition notifiers and the "adjust_jiffies"
353 * function. It is called twice on all CPU frequency changes that have
354 * external effects.
355 */
356void cpufreq_notify_transition(struct cpufreq_policy *policy,
357 struct cpufreq_freqs *freqs, unsigned int state)
358{
359 for_each_cpu(freqs->cpu, policy->cpus)
360 __cpufreq_notify_transition(policy, freqs, state);
361}
1da177e4
LT
362EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
363
364
1da177e4
LT
365/*********************************************************************
366 * SYSFS INTERFACE *
367 *********************************************************************/
368
3bcb09a3
JF
369static struct cpufreq_governor *__find_governor(const char *str_governor)
370{
371 struct cpufreq_governor *t;
372
373 list_for_each_entry(t, &cpufreq_governor_list, governor_list)
29464f28 374 if (!strnicmp(str_governor, t->name, CPUFREQ_NAME_LEN))
3bcb09a3
JF
375 return t;
376
377 return NULL;
378}
379
1da177e4
LT
380/**
381 * cpufreq_parse_governor - parse a governor string
382 */
905d77cd 383static int cpufreq_parse_governor(char *str_governor, unsigned int *policy,
1da177e4
LT
384 struct cpufreq_governor **governor)
385{
3bcb09a3 386 int err = -EINVAL;
1c3d85dd
RW
387
388 if (!cpufreq_driver)
3bcb09a3
JF
389 goto out;
390
1c3d85dd 391 if (cpufreq_driver->setpolicy) {
1da177e4
LT
392 if (!strnicmp(str_governor, "performance", CPUFREQ_NAME_LEN)) {
393 *policy = CPUFREQ_POLICY_PERFORMANCE;
3bcb09a3 394 err = 0;
e08f5f5b
GS
395 } else if (!strnicmp(str_governor, "powersave",
396 CPUFREQ_NAME_LEN)) {
1da177e4 397 *policy = CPUFREQ_POLICY_POWERSAVE;
3bcb09a3 398 err = 0;
1da177e4 399 }
9c0ebcf7 400 } else if (has_target()) {
1da177e4 401 struct cpufreq_governor *t;
3bcb09a3 402
3fc54d37 403 mutex_lock(&cpufreq_governor_mutex);
3bcb09a3
JF
404
405 t = __find_governor(str_governor);
406
ea714970 407 if (t == NULL) {
1a8e1463 408 int ret;
ea714970 409
1a8e1463
KC
410 mutex_unlock(&cpufreq_governor_mutex);
411 ret = request_module("cpufreq_%s", str_governor);
412 mutex_lock(&cpufreq_governor_mutex);
ea714970 413
1a8e1463
KC
414 if (ret == 0)
415 t = __find_governor(str_governor);
ea714970
JF
416 }
417
3bcb09a3
JF
418 if (t != NULL) {
419 *governor = t;
420 err = 0;
1da177e4 421 }
3bcb09a3 422
3fc54d37 423 mutex_unlock(&cpufreq_governor_mutex);
1da177e4 424 }
29464f28 425out:
3bcb09a3 426 return err;
1da177e4 427}
1da177e4 428
1da177e4 429/**
e08f5f5b
GS
430 * cpufreq_per_cpu_attr_read() / show_##file_name() -
431 * print out cpufreq information
1da177e4
LT
432 *
433 * Write out information from cpufreq_driver->policy[cpu]; object must be
434 * "unsigned int".
435 */
436
32ee8c3e
DJ
437#define show_one(file_name, object) \
438static ssize_t show_##file_name \
905d77cd 439(struct cpufreq_policy *policy, char *buf) \
32ee8c3e 440{ \
29464f28 441 return sprintf(buf, "%u\n", policy->object); \
1da177e4
LT
442}
443
444show_one(cpuinfo_min_freq, cpuinfo.min_freq);
445show_one(cpuinfo_max_freq, cpuinfo.max_freq);
ed129784 446show_one(cpuinfo_transition_latency, cpuinfo.transition_latency);
1da177e4
LT
447show_one(scaling_min_freq, min);
448show_one(scaling_max_freq, max);
449show_one(scaling_cur_freq, cur);
450
037ce839 451static int cpufreq_set_policy(struct cpufreq_policy *policy,
3a3e9e06 452 struct cpufreq_policy *new_policy);
7970e08b 453
1da177e4
LT
454/**
455 * cpufreq_per_cpu_attr_write() / store_##file_name() - sysfs write access
456 */
457#define store_one(file_name, object) \
458static ssize_t store_##file_name \
905d77cd 459(struct cpufreq_policy *policy, const char *buf, size_t count) \
1da177e4 460{ \
5136fa56 461 int ret; \
1da177e4
LT
462 struct cpufreq_policy new_policy; \
463 \
464 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
465 if (ret) \
466 return -EINVAL; \
467 \
29464f28 468 ret = sscanf(buf, "%u", &new_policy.object); \
1da177e4
LT
469 if (ret != 1) \
470 return -EINVAL; \
471 \
037ce839 472 ret = cpufreq_set_policy(policy, &new_policy); \
7970e08b 473 policy->user_policy.object = policy->object; \
1da177e4
LT
474 \
475 return ret ? ret : count; \
476}
477
29464f28
DJ
478store_one(scaling_min_freq, min);
479store_one(scaling_max_freq, max);
1da177e4
LT
480
481/**
482 * show_cpuinfo_cur_freq - current CPU frequency as detected by hardware
483 */
905d77cd
DJ
484static ssize_t show_cpuinfo_cur_freq(struct cpufreq_policy *policy,
485 char *buf)
1da177e4 486{
5a01f2e8 487 unsigned int cur_freq = __cpufreq_get(policy->cpu);
1da177e4
LT
488 if (!cur_freq)
489 return sprintf(buf, "<unknown>");
490 return sprintf(buf, "%u\n", cur_freq);
491}
492
1da177e4
LT
493/**
494 * show_scaling_governor - show the current policy for the specified CPU
495 */
905d77cd 496static ssize_t show_scaling_governor(struct cpufreq_policy *policy, char *buf)
1da177e4 497{
29464f28 498 if (policy->policy == CPUFREQ_POLICY_POWERSAVE)
1da177e4
LT
499 return sprintf(buf, "powersave\n");
500 else if (policy->policy == CPUFREQ_POLICY_PERFORMANCE)
501 return sprintf(buf, "performance\n");
502 else if (policy->governor)
4b972f0b 503 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n",
29464f28 504 policy->governor->name);
1da177e4
LT
505 return -EINVAL;
506}
507
1da177e4
LT
508/**
509 * store_scaling_governor - store policy for the specified CPU
510 */
905d77cd
DJ
511static ssize_t store_scaling_governor(struct cpufreq_policy *policy,
512 const char *buf, size_t count)
1da177e4 513{
5136fa56 514 int ret;
1da177e4
LT
515 char str_governor[16];
516 struct cpufreq_policy new_policy;
517
518 ret = cpufreq_get_policy(&new_policy, policy->cpu);
519 if (ret)
520 return ret;
521
29464f28 522 ret = sscanf(buf, "%15s", str_governor);
1da177e4
LT
523 if (ret != 1)
524 return -EINVAL;
525
e08f5f5b
GS
526 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
527 &new_policy.governor))
1da177e4
LT
528 return -EINVAL;
529
037ce839 530 ret = cpufreq_set_policy(policy, &new_policy);
7970e08b
TR
531
532 policy->user_policy.policy = policy->policy;
533 policy->user_policy.governor = policy->governor;
7970e08b 534
e08f5f5b
GS
535 if (ret)
536 return ret;
537 else
538 return count;
1da177e4
LT
539}
540
541/**
542 * show_scaling_driver - show the cpufreq driver currently loaded
543 */
905d77cd 544static ssize_t show_scaling_driver(struct cpufreq_policy *policy, char *buf)
1da177e4 545{
1c3d85dd 546 return scnprintf(buf, CPUFREQ_NAME_PLEN, "%s\n", cpufreq_driver->name);
1da177e4
LT
547}
548
549/**
550 * show_scaling_available_governors - show the available CPUfreq governors
551 */
905d77cd
DJ
552static ssize_t show_scaling_available_governors(struct cpufreq_policy *policy,
553 char *buf)
1da177e4
LT
554{
555 ssize_t i = 0;
556 struct cpufreq_governor *t;
557
9c0ebcf7 558 if (!has_target()) {
1da177e4
LT
559 i += sprintf(buf, "performance powersave");
560 goto out;
561 }
562
563 list_for_each_entry(t, &cpufreq_governor_list, governor_list) {
29464f28
DJ
564 if (i >= (ssize_t) ((PAGE_SIZE / sizeof(char))
565 - (CPUFREQ_NAME_LEN + 2)))
1da177e4 566 goto out;
4b972f0b 567 i += scnprintf(&buf[i], CPUFREQ_NAME_PLEN, "%s ", t->name);
1da177e4 568 }
7d5e350f 569out:
1da177e4
LT
570 i += sprintf(&buf[i], "\n");
571 return i;
572}
e8628dd0 573
f4fd3797 574ssize_t cpufreq_show_cpus(const struct cpumask *mask, char *buf)
1da177e4
LT
575{
576 ssize_t i = 0;
577 unsigned int cpu;
578
835481d9 579 for_each_cpu(cpu, mask) {
1da177e4
LT
580 if (i)
581 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), " ");
582 i += scnprintf(&buf[i], (PAGE_SIZE - i - 2), "%u", cpu);
583 if (i >= (PAGE_SIZE - 5))
29464f28 584 break;
1da177e4
LT
585 }
586 i += sprintf(&buf[i], "\n");
587 return i;
588}
f4fd3797 589EXPORT_SYMBOL_GPL(cpufreq_show_cpus);
1da177e4 590
e8628dd0
DW
591/**
592 * show_related_cpus - show the CPUs affected by each transition even if
593 * hw coordination is in use
594 */
595static ssize_t show_related_cpus(struct cpufreq_policy *policy, char *buf)
596{
f4fd3797 597 return cpufreq_show_cpus(policy->related_cpus, buf);
e8628dd0
DW
598}
599
600/**
601 * show_affected_cpus - show the CPUs affected by each transition
602 */
603static ssize_t show_affected_cpus(struct cpufreq_policy *policy, char *buf)
604{
f4fd3797 605 return cpufreq_show_cpus(policy->cpus, buf);
e8628dd0
DW
606}
607
9e76988e 608static ssize_t store_scaling_setspeed(struct cpufreq_policy *policy,
905d77cd 609 const char *buf, size_t count)
9e76988e
VP
610{
611 unsigned int freq = 0;
612 unsigned int ret;
613
879000f9 614 if (!policy->governor || !policy->governor->store_setspeed)
9e76988e
VP
615 return -EINVAL;
616
617 ret = sscanf(buf, "%u", &freq);
618 if (ret != 1)
619 return -EINVAL;
620
621 policy->governor->store_setspeed(policy, freq);
622
623 return count;
624}
625
626static ssize_t show_scaling_setspeed(struct cpufreq_policy *policy, char *buf)
627{
879000f9 628 if (!policy->governor || !policy->governor->show_setspeed)
9e76988e
VP
629 return sprintf(buf, "<unsupported>\n");
630
631 return policy->governor->show_setspeed(policy, buf);
632}
1da177e4 633
e2f74f35 634/**
8bf1ac72 635 * show_bios_limit - show the current cpufreq HW/BIOS limitation
e2f74f35
TR
636 */
637static ssize_t show_bios_limit(struct cpufreq_policy *policy, char *buf)
638{
639 unsigned int limit;
640 int ret;
1c3d85dd
RW
641 if (cpufreq_driver->bios_limit) {
642 ret = cpufreq_driver->bios_limit(policy->cpu, &limit);
e2f74f35
TR
643 if (!ret)
644 return sprintf(buf, "%u\n", limit);
645 }
646 return sprintf(buf, "%u\n", policy->cpuinfo.max_freq);
647}
648
6dad2a29
BP
649cpufreq_freq_attr_ro_perm(cpuinfo_cur_freq, 0400);
650cpufreq_freq_attr_ro(cpuinfo_min_freq);
651cpufreq_freq_attr_ro(cpuinfo_max_freq);
652cpufreq_freq_attr_ro(cpuinfo_transition_latency);
653cpufreq_freq_attr_ro(scaling_available_governors);
654cpufreq_freq_attr_ro(scaling_driver);
655cpufreq_freq_attr_ro(scaling_cur_freq);
656cpufreq_freq_attr_ro(bios_limit);
657cpufreq_freq_attr_ro(related_cpus);
658cpufreq_freq_attr_ro(affected_cpus);
659cpufreq_freq_attr_rw(scaling_min_freq);
660cpufreq_freq_attr_rw(scaling_max_freq);
661cpufreq_freq_attr_rw(scaling_governor);
662cpufreq_freq_attr_rw(scaling_setspeed);
1da177e4 663
905d77cd 664static struct attribute *default_attrs[] = {
1da177e4
LT
665 &cpuinfo_min_freq.attr,
666 &cpuinfo_max_freq.attr,
ed129784 667 &cpuinfo_transition_latency.attr,
1da177e4
LT
668 &scaling_min_freq.attr,
669 &scaling_max_freq.attr,
670 &affected_cpus.attr,
e8628dd0 671 &related_cpus.attr,
1da177e4
LT
672 &scaling_governor.attr,
673 &scaling_driver.attr,
674 &scaling_available_governors.attr,
9e76988e 675 &scaling_setspeed.attr,
1da177e4
LT
676 NULL
677};
678
29464f28
DJ
679#define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
680#define to_attr(a) container_of(a, struct freq_attr, attr)
1da177e4 681
29464f28 682static ssize_t show(struct kobject *kobj, struct attribute *attr, char *buf)
1da177e4 683{
905d77cd
DJ
684 struct cpufreq_policy *policy = to_policy(kobj);
685 struct freq_attr *fattr = to_attr(attr);
1b750e3b 686 ssize_t ret;
6eed9404
VK
687
688 if (!down_read_trylock(&cpufreq_rwsem))
1b750e3b 689 return -EINVAL;
5a01f2e8 690
1b750e3b 691 lock_policy_rwsem_read(policy->cpu);
5a01f2e8 692
e08f5f5b
GS
693 if (fattr->show)
694 ret = fattr->show(policy, buf);
695 else
696 ret = -EIO;
697
5a01f2e8 698 unlock_policy_rwsem_read(policy->cpu);
6eed9404 699 up_read(&cpufreq_rwsem);
1b750e3b 700
1da177e4
LT
701 return ret;
702}
703
905d77cd
DJ
704static ssize_t store(struct kobject *kobj, struct attribute *attr,
705 const char *buf, size_t count)
1da177e4 706{
905d77cd
DJ
707 struct cpufreq_policy *policy = to_policy(kobj);
708 struct freq_attr *fattr = to_attr(attr);
a07530b4 709 ssize_t ret = -EINVAL;
6eed9404 710
4f750c93
SB
711 get_online_cpus();
712
713 if (!cpu_online(policy->cpu))
714 goto unlock;
715
6eed9404 716 if (!down_read_trylock(&cpufreq_rwsem))
4f750c93 717 goto unlock;
5a01f2e8 718
1b750e3b 719 lock_policy_rwsem_write(policy->cpu);
5a01f2e8 720
e08f5f5b
GS
721 if (fattr->store)
722 ret = fattr->store(policy, buf, count);
723 else
724 ret = -EIO;
725
5a01f2e8 726 unlock_policy_rwsem_write(policy->cpu);
6eed9404 727
6eed9404 728 up_read(&cpufreq_rwsem);
4f750c93
SB
729unlock:
730 put_online_cpus();
731
1da177e4
LT
732 return ret;
733}
734
905d77cd 735static void cpufreq_sysfs_release(struct kobject *kobj)
1da177e4 736{
905d77cd 737 struct cpufreq_policy *policy = to_policy(kobj);
2d06d8c4 738 pr_debug("last reference is dropped\n");
1da177e4
LT
739 complete(&policy->kobj_unregister);
740}
741
52cf25d0 742static const struct sysfs_ops sysfs_ops = {
1da177e4
LT
743 .show = show,
744 .store = store,
745};
746
747static struct kobj_type ktype_cpufreq = {
748 .sysfs_ops = &sysfs_ops,
749 .default_attrs = default_attrs,
750 .release = cpufreq_sysfs_release,
751};
752
2361be23
VK
753struct kobject *cpufreq_global_kobject;
754EXPORT_SYMBOL(cpufreq_global_kobject);
755
756static int cpufreq_global_kobject_usage;
757
758int cpufreq_get_global_kobject(void)
759{
760 if (!cpufreq_global_kobject_usage++)
761 return kobject_add(cpufreq_global_kobject,
762 &cpu_subsys.dev_root->kobj, "%s", "cpufreq");
763
764 return 0;
765}
766EXPORT_SYMBOL(cpufreq_get_global_kobject);
767
768void cpufreq_put_global_kobject(void)
769{
770 if (!--cpufreq_global_kobject_usage)
771 kobject_del(cpufreq_global_kobject);
772}
773EXPORT_SYMBOL(cpufreq_put_global_kobject);
774
775int cpufreq_sysfs_create_file(const struct attribute *attr)
776{
777 int ret = cpufreq_get_global_kobject();
778
779 if (!ret) {
780 ret = sysfs_create_file(cpufreq_global_kobject, attr);
781 if (ret)
782 cpufreq_put_global_kobject();
783 }
784
785 return ret;
786}
787EXPORT_SYMBOL(cpufreq_sysfs_create_file);
788
789void cpufreq_sysfs_remove_file(const struct attribute *attr)
790{
791 sysfs_remove_file(cpufreq_global_kobject, attr);
792 cpufreq_put_global_kobject();
793}
794EXPORT_SYMBOL(cpufreq_sysfs_remove_file);
795
19d6f7ec 796/* symlink affected CPUs */
308b60e7 797static int cpufreq_add_dev_symlink(struct cpufreq_policy *policy)
19d6f7ec
DJ
798{
799 unsigned int j;
800 int ret = 0;
801
802 for_each_cpu(j, policy->cpus) {
8a25a2fd 803 struct device *cpu_dev;
19d6f7ec 804
308b60e7 805 if (j == policy->cpu)
19d6f7ec 806 continue;
19d6f7ec 807
e8fdde10 808 pr_debug("Adding link for CPU: %u\n", j);
8a25a2fd
KS
809 cpu_dev = get_cpu_device(j);
810 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
19d6f7ec 811 "cpufreq");
71c3461e
RW
812 if (ret)
813 break;
19d6f7ec
DJ
814 }
815 return ret;
816}
817
308b60e7 818static int cpufreq_add_dev_interface(struct cpufreq_policy *policy,
8a25a2fd 819 struct device *dev)
909a694e
DJ
820{
821 struct freq_attr **drv_attr;
909a694e 822 int ret = 0;
909a694e
DJ
823
824 /* prepare interface data */
825 ret = kobject_init_and_add(&policy->kobj, &ktype_cpufreq,
8a25a2fd 826 &dev->kobj, "cpufreq");
909a694e
DJ
827 if (ret)
828 return ret;
829
830 /* set up files for this cpu device */
1c3d85dd 831 drv_attr = cpufreq_driver->attr;
909a694e
DJ
832 while ((drv_attr) && (*drv_attr)) {
833 ret = sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
834 if (ret)
1c3d85dd 835 goto err_out_kobj_put;
909a694e
DJ
836 drv_attr++;
837 }
1c3d85dd 838 if (cpufreq_driver->get) {
909a694e
DJ
839 ret = sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
840 if (ret)
1c3d85dd 841 goto err_out_kobj_put;
909a694e 842 }
9c0ebcf7 843 if (has_target()) {
909a694e
DJ
844 ret = sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
845 if (ret)
1c3d85dd 846 goto err_out_kobj_put;
909a694e 847 }
1c3d85dd 848 if (cpufreq_driver->bios_limit) {
e2f74f35
TR
849 ret = sysfs_create_file(&policy->kobj, &bios_limit.attr);
850 if (ret)
1c3d85dd 851 goto err_out_kobj_put;
e2f74f35 852 }
909a694e 853
308b60e7 854 ret = cpufreq_add_dev_symlink(policy);
ecf7e461
DJ
855 if (ret)
856 goto err_out_kobj_put;
857
e18f1682
SB
858 return ret;
859
860err_out_kobj_put:
861 kobject_put(&policy->kobj);
862 wait_for_completion(&policy->kobj_unregister);
863 return ret;
864}
865
866static void cpufreq_init_policy(struct cpufreq_policy *policy)
867{
868 struct cpufreq_policy new_policy;
869 int ret = 0;
870
d5b73cd8 871 memcpy(&new_policy, policy, sizeof(*policy));
037ce839 872 /* assure that the starting sequence is run in cpufreq_set_policy */
ecf7e461
DJ
873 policy->governor = NULL;
874
875 /* set default policy */
037ce839 876 ret = cpufreq_set_policy(policy, &new_policy);
ecf7e461
DJ
877 policy->user_policy.policy = policy->policy;
878 policy->user_policy.governor = policy->governor;
879
880 if (ret) {
2d06d8c4 881 pr_debug("setting policy failed\n");
1c3d85dd
RW
882 if (cpufreq_driver->exit)
883 cpufreq_driver->exit(policy);
ecf7e461 884 }
909a694e
DJ
885}
886
fcf80582 887#ifdef CONFIG_HOTPLUG_CPU
d8d3b471
VK
888static int cpufreq_add_policy_cpu(struct cpufreq_policy *policy,
889 unsigned int cpu, struct device *dev,
890 bool frozen)
fcf80582 891{
9c0ebcf7 892 int ret = 0;
fcf80582
VK
893 unsigned long flags;
894
9c0ebcf7 895 if (has_target()) {
3de9bdeb
VK
896 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
897 if (ret) {
898 pr_err("%s: Failed to stop governor\n", __func__);
899 return ret;
900 }
901 }
fcf80582 902
d8d3b471 903 lock_policy_rwsem_write(policy->cpu);
2eaa3e2d 904
0d1857a1 905 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 906
fcf80582
VK
907 cpumask_set_cpu(cpu, policy->cpus);
908 per_cpu(cpufreq_cpu_data, cpu) = policy;
0d1857a1 909 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
fcf80582 910
d8d3b471 911 unlock_policy_rwsem_write(policy->cpu);
2eaa3e2d 912
9c0ebcf7 913 if (has_target()) {
3de9bdeb
VK
914 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
915 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
916 pr_err("%s: Failed to start governor\n", __func__);
917 return ret;
918 }
820c6ca2 919 }
fcf80582 920
a82fab29 921 /* Don't touch sysfs links during light-weight init */
71c3461e
RW
922 if (!frozen)
923 ret = sysfs_create_link(&dev->kobj, &policy->kobj, "cpufreq");
a82fab29
SB
924
925 return ret;
fcf80582
VK
926}
927#endif
1da177e4 928
8414809c
SB
929static struct cpufreq_policy *cpufreq_policy_restore(unsigned int cpu)
930{
931 struct cpufreq_policy *policy;
932 unsigned long flags;
933
44871c9c 934 read_lock_irqsave(&cpufreq_driver_lock, flags);
8414809c
SB
935
936 policy = per_cpu(cpufreq_cpu_data_fallback, cpu);
937
44871c9c 938 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
8414809c
SB
939
940 return policy;
941}
942
e9698cc5
SB
943static struct cpufreq_policy *cpufreq_policy_alloc(void)
944{
945 struct cpufreq_policy *policy;
946
947 policy = kzalloc(sizeof(*policy), GFP_KERNEL);
948 if (!policy)
949 return NULL;
950
951 if (!alloc_cpumask_var(&policy->cpus, GFP_KERNEL))
952 goto err_free_policy;
953
954 if (!zalloc_cpumask_var(&policy->related_cpus, GFP_KERNEL))
955 goto err_free_cpumask;
956
c88a1f8b 957 INIT_LIST_HEAD(&policy->policy_list);
e9698cc5
SB
958 return policy;
959
960err_free_cpumask:
961 free_cpumask_var(policy->cpus);
962err_free_policy:
963 kfree(policy);
964
965 return NULL;
966}
967
968static void cpufreq_policy_free(struct cpufreq_policy *policy)
969{
970 free_cpumask_var(policy->related_cpus);
971 free_cpumask_var(policy->cpus);
972 kfree(policy);
973}
974
0d66b91e
SB
975static void update_policy_cpu(struct cpufreq_policy *policy, unsigned int cpu)
976{
99ec899e 977 if (WARN_ON(cpu == policy->cpu))
cb38ed5c
SB
978 return;
979
8efd5765
VK
980 /*
981 * Take direct locks as lock_policy_rwsem_write wouldn't work here.
982 * Also lock for last cpu is enough here as contention will happen only
983 * after policy->cpu is changed and after it is changed, other threads
984 * will try to acquire lock for new cpu. And policy is already updated
985 * by then.
986 */
987 down_write(&per_cpu(cpu_policy_rwsem, policy->cpu));
988
0d66b91e
SB
989 policy->last_cpu = policy->cpu;
990 policy->cpu = cpu;
991
8efd5765
VK
992 up_write(&per_cpu(cpu_policy_rwsem, policy->last_cpu));
993
0d66b91e 994 cpufreq_frequency_table_update_policy_cpu(policy);
0d66b91e
SB
995 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
996 CPUFREQ_UPDATE_POLICY_CPU, policy);
997}
998
a82fab29
SB
999static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1000 bool frozen)
1da177e4 1001{
fcf80582 1002 unsigned int j, cpu = dev->id;
65922465 1003 int ret = -ENOMEM;
1da177e4 1004 struct cpufreq_policy *policy;
1da177e4 1005 unsigned long flags;
90e41bac 1006#ifdef CONFIG_HOTPLUG_CPU
1b274294 1007 struct cpufreq_policy *tpolicy;
fcf80582 1008 struct cpufreq_governor *gov;
90e41bac 1009#endif
1da177e4 1010
c32b6b8e
AR
1011 if (cpu_is_offline(cpu))
1012 return 0;
1013
2d06d8c4 1014 pr_debug("adding CPU %u\n", cpu);
1da177e4
LT
1015
1016#ifdef CONFIG_SMP
1017 /* check whether a different CPU already registered this
1018 * CPU because it is in the same boat. */
1019 policy = cpufreq_cpu_get(cpu);
1020 if (unlikely(policy)) {
8ff69732 1021 cpufreq_cpu_put(policy);
1da177e4
LT
1022 return 0;
1023 }
5025d628 1024#endif
fcf80582 1025
6eed9404
VK
1026 if (!down_read_trylock(&cpufreq_rwsem))
1027 return 0;
1028
fcf80582
VK
1029#ifdef CONFIG_HOTPLUG_CPU
1030 /* Check if this cpu was hot-unplugged earlier and has siblings */
0d1857a1 1031 read_lock_irqsave(&cpufreq_driver_lock, flags);
1b274294
VK
1032 list_for_each_entry(tpolicy, &cpufreq_policy_list, policy_list) {
1033 if (cpumask_test_cpu(cpu, tpolicy->related_cpus)) {
0d1857a1 1034 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1b274294 1035 ret = cpufreq_add_policy_cpu(tpolicy, cpu, dev, frozen);
6eed9404
VK
1036 up_read(&cpufreq_rwsem);
1037 return ret;
2eaa3e2d 1038 }
fcf80582 1039 }
0d1857a1 1040 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4
LT
1041#endif
1042
8414809c
SB
1043 if (frozen)
1044 /* Restore the saved policy when doing light-weight init */
1045 policy = cpufreq_policy_restore(cpu);
1046 else
1047 policy = cpufreq_policy_alloc();
1048
059019a3 1049 if (!policy)
1da177e4 1050 goto nomem_out;
059019a3 1051
0d66b91e
SB
1052
1053 /*
1054 * In the resume path, since we restore a saved policy, the assignment
1055 * to policy->cpu is like an update of the existing policy, rather than
1056 * the creation of a brand new one. So we need to perform this update
1057 * by invoking update_policy_cpu().
1058 */
1059 if (frozen && cpu != policy->cpu)
1060 update_policy_cpu(policy, cpu);
1061 else
1062 policy->cpu = cpu;
1063
65922465 1064 policy->governor = CPUFREQ_DEFAULT_GOVERNOR;
835481d9 1065 cpumask_copy(policy->cpus, cpumask_of(cpu));
1da177e4 1066
1da177e4 1067 init_completion(&policy->kobj_unregister);
65f27f38 1068 INIT_WORK(&policy->update, handle_update);
1da177e4
LT
1069
1070 /* call driver. From then on the cpufreq must be able
1071 * to accept all calls to ->verify and ->setpolicy for this CPU
1072 */
1c3d85dd 1073 ret = cpufreq_driver->init(policy);
1da177e4 1074 if (ret) {
2d06d8c4 1075 pr_debug("initialization failed\n");
2eaa3e2d 1076 goto err_set_policy_cpu;
1da177e4 1077 }
643ae6e8 1078
da60ce9f
VK
1079 if (cpufreq_driver->get) {
1080 policy->cur = cpufreq_driver->get(policy->cpu);
1081 if (!policy->cur) {
1082 pr_err("%s: ->get() failed\n", __func__);
1083 goto err_get_freq;
1084 }
1085 }
1086
fcf80582
VK
1087 /* related cpus should atleast have policy->cpus */
1088 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1089
643ae6e8
VK
1090 /*
1091 * affected cpus must always be the one, which are online. We aren't
1092 * managing offline cpus here.
1093 */
1094 cpumask_and(policy->cpus, policy->cpus, cpu_online_mask);
1095
187d9f4e
MC
1096 policy->user_policy.min = policy->min;
1097 policy->user_policy.max = policy->max;
1da177e4 1098
a1531acd
TR
1099 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1100 CPUFREQ_START, policy);
1101
fcf80582
VK
1102#ifdef CONFIG_HOTPLUG_CPU
1103 gov = __find_governor(per_cpu(cpufreq_cpu_governor, cpu));
1104 if (gov) {
1105 policy->governor = gov;
1106 pr_debug("Restoring governor %s for cpu %d\n",
1107 policy->governor->name, cpu);
4bfa042c 1108 }
fcf80582 1109#endif
1da177e4 1110
e18f1682 1111 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1112 for_each_cpu(j, policy->cpus)
e18f1682 1113 per_cpu(cpufreq_cpu_data, j) = policy;
e18f1682
SB
1114 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1115
a82fab29 1116 if (!frozen) {
308b60e7 1117 ret = cpufreq_add_dev_interface(policy, dev);
a82fab29
SB
1118 if (ret)
1119 goto err_out_unregister;
1120 }
8ff69732 1121
9515f4d6
VK
1122 write_lock_irqsave(&cpufreq_driver_lock, flags);
1123 list_add(&policy->policy_list, &cpufreq_policy_list);
1124 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1125
e18f1682
SB
1126 cpufreq_init_policy(policy);
1127
038c5b3e 1128 kobject_uevent(&policy->kobj, KOBJ_ADD);
6eed9404
VK
1129 up_read(&cpufreq_rwsem);
1130
2d06d8c4 1131 pr_debug("initialization complete\n");
87c32271 1132
1da177e4
LT
1133 return 0;
1134
1da177e4 1135err_out_unregister:
0d1857a1 1136 write_lock_irqsave(&cpufreq_driver_lock, flags);
474deff7 1137 for_each_cpu(j, policy->cpus)
7a6aedfa 1138 per_cpu(cpufreq_cpu_data, j) = NULL;
0d1857a1 1139 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1140
da60ce9f
VK
1141err_get_freq:
1142 if (cpufreq_driver->exit)
1143 cpufreq_driver->exit(policy);
2eaa3e2d 1144err_set_policy_cpu:
e9698cc5 1145 cpufreq_policy_free(policy);
1da177e4 1146nomem_out:
6eed9404
VK
1147 up_read(&cpufreq_rwsem);
1148
1da177e4
LT
1149 return ret;
1150}
1151
a82fab29
SB
1152/**
1153 * cpufreq_add_dev - add a CPU device
1154 *
1155 * Adds the cpufreq interface for a CPU device.
1156 *
1157 * The Oracle says: try running cpufreq registration/unregistration concurrently
1158 * with with cpu hotplugging and all hell will break loose. Tried to clean this
1159 * mess up, but more thorough testing is needed. - Mathieu
1160 */
1161static int cpufreq_add_dev(struct device *dev, struct subsys_interface *sif)
1162{
1163 return __cpufreq_add_dev(dev, sif, false);
1164}
1165
3a3e9e06 1166static int cpufreq_nominate_new_policy_cpu(struct cpufreq_policy *policy,
a82fab29 1167 unsigned int old_cpu, bool frozen)
f9ba680d
SB
1168{
1169 struct device *cpu_dev;
f9ba680d
SB
1170 int ret;
1171
1172 /* first sibling now owns the new sysfs dir */
9c8f1ee4 1173 cpu_dev = get_cpu_device(cpumask_any_but(policy->cpus, old_cpu));
a82fab29
SB
1174
1175 /* Don't touch sysfs files during light-weight tear-down */
1176 if (frozen)
1177 return cpu_dev->id;
1178
f9ba680d 1179 sysfs_remove_link(&cpu_dev->kobj, "cpufreq");
3a3e9e06 1180 ret = kobject_move(&policy->kobj, &cpu_dev->kobj);
f9ba680d
SB
1181 if (ret) {
1182 pr_err("%s: Failed to move kobj: %d", __func__, ret);
1183
1b750e3b 1184 lock_policy_rwsem_write(old_cpu);
3a3e9e06 1185 cpumask_set_cpu(old_cpu, policy->cpus);
f9ba680d
SB
1186 unlock_policy_rwsem_write(old_cpu);
1187
3a3e9e06 1188 ret = sysfs_create_link(&cpu_dev->kobj, &policy->kobj,
f9ba680d
SB
1189 "cpufreq");
1190
1191 return -EINVAL;
1192 }
1193
1194 return cpu_dev->id;
1195}
1196
cedb70af
SB
1197static int __cpufreq_remove_dev_prepare(struct device *dev,
1198 struct subsys_interface *sif,
1199 bool frozen)
1da177e4 1200{
f9ba680d 1201 unsigned int cpu = dev->id, cpus;
3de9bdeb 1202 int new_cpu, ret;
1da177e4 1203 unsigned long flags;
3a3e9e06 1204 struct cpufreq_policy *policy;
1da177e4 1205
b8eed8af 1206 pr_debug("%s: unregistering CPU %u\n", __func__, cpu);
1da177e4 1207
0d1857a1 1208 write_lock_irqsave(&cpufreq_driver_lock, flags);
2eaa3e2d 1209
3a3e9e06 1210 policy = per_cpu(cpufreq_cpu_data, cpu);
2eaa3e2d 1211
8414809c
SB
1212 /* Save the policy somewhere when doing a light-weight tear-down */
1213 if (frozen)
3a3e9e06 1214 per_cpu(cpufreq_cpu_data_fallback, cpu) = policy;
8414809c 1215
0d1857a1 1216 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 1217
3a3e9e06 1218 if (!policy) {
b8eed8af 1219 pr_debug("%s: No cpu_data found\n", __func__);
1da177e4
LT
1220 return -EINVAL;
1221 }
1da177e4 1222
9c0ebcf7 1223 if (has_target()) {
3de9bdeb
VK
1224 ret = __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1225 if (ret) {
1226 pr_err("%s: Failed to stop governor\n", __func__);
1227 return ret;
1228 }
1229 }
1da177e4 1230
084f3493 1231#ifdef CONFIG_HOTPLUG_CPU
1c3d85dd 1232 if (!cpufreq_driver->setpolicy)
fa69e33f 1233 strncpy(per_cpu(cpufreq_cpu_governor, cpu),
3a3e9e06 1234 policy->governor->name, CPUFREQ_NAME_LEN);
1da177e4
LT
1235#endif
1236
9c8f1ee4 1237 lock_policy_rwsem_read(cpu);
3a3e9e06 1238 cpus = cpumask_weight(policy->cpus);
9c8f1ee4 1239 unlock_policy_rwsem_read(cpu);
084f3493 1240
61173f25
SB
1241 if (cpu != policy->cpu) {
1242 if (!frozen)
1243 sysfs_remove_link(&dev->kobj, "cpufreq");
73bf0fc2 1244 } else if (cpus > 1) {
3a3e9e06 1245 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu, frozen);
f9ba680d 1246 if (new_cpu >= 0) {
3a3e9e06 1247 update_policy_cpu(policy, new_cpu);
a82fab29
SB
1248
1249 if (!frozen) {
75949c9a
VK
1250 pr_debug("%s: policy Kobject moved to cpu: %d from: %d\n",
1251 __func__, new_cpu, cpu);
a82fab29 1252 }
1da177e4
LT
1253 }
1254 }
1da177e4 1255
cedb70af
SB
1256 return 0;
1257}
1258
1259static int __cpufreq_remove_dev_finish(struct device *dev,
1260 struct subsys_interface *sif,
1261 bool frozen)
1262{
1263 unsigned int cpu = dev->id, cpus;
1264 int ret;
1265 unsigned long flags;
1266 struct cpufreq_policy *policy;
1267 struct kobject *kobj;
1268 struct completion *cmp;
1269
1270 read_lock_irqsave(&cpufreq_driver_lock, flags);
1271 policy = per_cpu(cpufreq_cpu_data, cpu);
1272 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1273
1274 if (!policy) {
1275 pr_debug("%s: No cpu_data found\n", __func__);
1276 return -EINVAL;
1277 }
1278
1b750e3b 1279 lock_policy_rwsem_write(cpu);
cedb70af 1280 cpus = cpumask_weight(policy->cpus);
9c8f1ee4
VK
1281
1282 if (cpus > 1)
1283 cpumask_clear_cpu(cpu, policy->cpus);
1284 unlock_policy_rwsem_write(cpu);
cedb70af 1285
b8eed8af
VK
1286 /* If cpu is last user of policy, free policy */
1287 if (cpus == 1) {
9c0ebcf7 1288 if (has_target()) {
3de9bdeb
VK
1289 ret = __cpufreq_governor(policy,
1290 CPUFREQ_GOV_POLICY_EXIT);
1291 if (ret) {
1292 pr_err("%s: Failed to exit governor\n",
1293 __func__);
1294 return ret;
1295 }
edab2fbc 1296 }
2a998599 1297
8414809c
SB
1298 if (!frozen) {
1299 lock_policy_rwsem_read(cpu);
3a3e9e06
VK
1300 kobj = &policy->kobj;
1301 cmp = &policy->kobj_unregister;
8414809c
SB
1302 unlock_policy_rwsem_read(cpu);
1303 kobject_put(kobj);
1304
1305 /*
1306 * We need to make sure that the underlying kobj is
1307 * actually not referenced anymore by anybody before we
1308 * proceed with unloading.
1309 */
1310 pr_debug("waiting for dropping of refcount\n");
1311 wait_for_completion(cmp);
1312 pr_debug("wait complete\n");
1313 }
7d26e2d5 1314
8414809c
SB
1315 /*
1316 * Perform the ->exit() even during light-weight tear-down,
1317 * since this is a core component, and is essential for the
1318 * subsequent light-weight ->init() to succeed.
b8eed8af 1319 */
1c3d85dd 1320 if (cpufreq_driver->exit)
3a3e9e06 1321 cpufreq_driver->exit(policy);
27ecddc2 1322
9515f4d6
VK
1323 /* Remove policy from list of active policies */
1324 write_lock_irqsave(&cpufreq_driver_lock, flags);
1325 list_del(&policy->policy_list);
1326 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1327
8414809c 1328 if (!frozen)
3a3e9e06 1329 cpufreq_policy_free(policy);
2a998599 1330 } else {
9c0ebcf7 1331 if (has_target()) {
3de9bdeb
VK
1332 if ((ret = __cpufreq_governor(policy, CPUFREQ_GOV_START)) ||
1333 (ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS))) {
1334 pr_err("%s: Failed to start governor\n",
1335 __func__);
1336 return ret;
1337 }
2a998599 1338 }
27ecddc2 1339 }
1da177e4 1340
474deff7 1341 per_cpu(cpufreq_cpu_data, cpu) = NULL;
1da177e4
LT
1342 return 0;
1343}
1344
cedb70af 1345/**
27a862e9 1346 * cpufreq_remove_dev - remove a CPU device
cedb70af
SB
1347 *
1348 * Removes the cpufreq interface for a CPU device.
cedb70af 1349 */
8a25a2fd 1350static int cpufreq_remove_dev(struct device *dev, struct subsys_interface *sif)
5a01f2e8 1351{
8a25a2fd 1352 unsigned int cpu = dev->id;
27a862e9 1353 int ret;
ec28297a
VP
1354
1355 if (cpu_is_offline(cpu))
1356 return 0;
1357
27a862e9
VK
1358 ret = __cpufreq_remove_dev_prepare(dev, sif, false);
1359
1360 if (!ret)
1361 ret = __cpufreq_remove_dev_finish(dev, sif, false);
1362
1363 return ret;
5a01f2e8
VP
1364}
1365
65f27f38 1366static void handle_update(struct work_struct *work)
1da177e4 1367{
65f27f38
DH
1368 struct cpufreq_policy *policy =
1369 container_of(work, struct cpufreq_policy, update);
1370 unsigned int cpu = policy->cpu;
2d06d8c4 1371 pr_debug("handle_update for cpu %u called\n", cpu);
1da177e4
LT
1372 cpufreq_update_policy(cpu);
1373}
1374
1375/**
bb176f7d
VK
1376 * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're
1377 * in deep trouble.
1da177e4
LT
1378 * @cpu: cpu number
1379 * @old_freq: CPU frequency the kernel thinks the CPU runs at
1380 * @new_freq: CPU frequency the CPU actually runs at
1381 *
29464f28
DJ
1382 * We adjust to current frequency first, and need to clean up later.
1383 * So either call to cpufreq_update_policy() or schedule handle_update()).
1da177e4 1384 */
e08f5f5b
GS
1385static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
1386 unsigned int new_freq)
1da177e4 1387{
b43a7ffb 1388 struct cpufreq_policy *policy;
1da177e4 1389 struct cpufreq_freqs freqs;
b43a7ffb
VK
1390 unsigned long flags;
1391
2d06d8c4 1392 pr_debug("Warning: CPU frequency out of sync: cpufreq and timing "
1da177e4
LT
1393 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1394
1da177e4
LT
1395 freqs.old = old_freq;
1396 freqs.new = new_freq;
b43a7ffb
VK
1397
1398 read_lock_irqsave(&cpufreq_driver_lock, flags);
1399 policy = per_cpu(cpufreq_cpu_data, cpu);
1400 read_unlock_irqrestore(&cpufreq_driver_lock, flags);
1401
1402 cpufreq_notify_transition(policy, &freqs, CPUFREQ_PRECHANGE);
1403 cpufreq_notify_transition(policy, &freqs, CPUFREQ_POSTCHANGE);
1da177e4
LT
1404}
1405
32ee8c3e 1406/**
4ab70df4 1407 * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
95235ca2
VP
1408 * @cpu: CPU number
1409 *
1410 * This is the last known freq, without actually getting it from the driver.
1411 * Return value will be same as what is shown in scaling_cur_freq in sysfs.
1412 */
1413unsigned int cpufreq_quick_get(unsigned int cpu)
1414{
9e21ba8b 1415 struct cpufreq_policy *policy;
e08f5f5b 1416 unsigned int ret_freq = 0;
95235ca2 1417
1c3d85dd
RW
1418 if (cpufreq_driver && cpufreq_driver->setpolicy && cpufreq_driver->get)
1419 return cpufreq_driver->get(cpu);
9e21ba8b
DB
1420
1421 policy = cpufreq_cpu_get(cpu);
95235ca2 1422 if (policy) {
e08f5f5b 1423 ret_freq = policy->cur;
95235ca2
VP
1424 cpufreq_cpu_put(policy);
1425 }
1426
4d34a67d 1427 return ret_freq;
95235ca2
VP
1428}
1429EXPORT_SYMBOL(cpufreq_quick_get);
1430
3d737108
JB
1431/**
1432 * cpufreq_quick_get_max - get the max reported CPU frequency for this CPU
1433 * @cpu: CPU number
1434 *
1435 * Just return the max possible frequency for a given CPU.
1436 */
1437unsigned int cpufreq_quick_get_max(unsigned int cpu)
1438{
1439 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1440 unsigned int ret_freq = 0;
1441
1442 if (policy) {
1443 ret_freq = policy->max;
1444 cpufreq_cpu_put(policy);
1445 }
1446
1447 return ret_freq;
1448}
1449EXPORT_SYMBOL(cpufreq_quick_get_max);
1450
5a01f2e8 1451static unsigned int __cpufreq_get(unsigned int cpu)
1da177e4 1452{
7a6aedfa 1453 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
e08f5f5b 1454 unsigned int ret_freq = 0;
5800043b 1455
1c3d85dd 1456 if (!cpufreq_driver->get)
4d34a67d 1457 return ret_freq;
1da177e4 1458
1c3d85dd 1459 ret_freq = cpufreq_driver->get(cpu);
1da177e4 1460
e08f5f5b 1461 if (ret_freq && policy->cur &&
1c3d85dd 1462 !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
e08f5f5b
GS
1463 /* verify no discrepancy between actual and
1464 saved value exists */
1465 if (unlikely(ret_freq != policy->cur)) {
1466 cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
1da177e4
LT
1467 schedule_work(&policy->update);
1468 }
1469 }
1470
4d34a67d 1471 return ret_freq;
5a01f2e8 1472}
1da177e4 1473
5a01f2e8
VP
1474/**
1475 * cpufreq_get - get the current CPU frequency (in kHz)
1476 * @cpu: CPU number
1477 *
1478 * Get the CPU current (static) CPU frequency
1479 */
1480unsigned int cpufreq_get(unsigned int cpu)
1481{
1482 unsigned int ret_freq = 0;
5a01f2e8 1483
26ca8694
VK
1484 if (cpufreq_disabled() || !cpufreq_driver)
1485 return -ENOENT;
1486
6eed9404
VK
1487 if (!down_read_trylock(&cpufreq_rwsem))
1488 return 0;
5a01f2e8 1489
1b750e3b 1490 lock_policy_rwsem_read(cpu);
5a01f2e8
VP
1491
1492 ret_freq = __cpufreq_get(cpu);
1493
1494 unlock_policy_rwsem_read(cpu);
6eed9404
VK
1495 up_read(&cpufreq_rwsem);
1496
4d34a67d 1497 return ret_freq;
1da177e4
LT
1498}
1499EXPORT_SYMBOL(cpufreq_get);
1500
8a25a2fd
KS
1501static struct subsys_interface cpufreq_interface = {
1502 .name = "cpufreq",
1503 .subsys = &cpu_subsys,
1504 .add_dev = cpufreq_add_dev,
1505 .remove_dev = cpufreq_remove_dev,
e00e56df
RW
1506};
1507
42d4dc3f 1508/**
e00e56df
RW
1509 * cpufreq_bp_suspend - Prepare the boot CPU for system suspend.
1510 *
1511 * This function is only executed for the boot processor. The other CPUs
1512 * have been put offline by means of CPU hotplug.
42d4dc3f 1513 */
e00e56df 1514static int cpufreq_bp_suspend(void)
42d4dc3f 1515{
e08f5f5b 1516 int ret = 0;
4bc5d341 1517
e00e56df 1518 int cpu = smp_processor_id();
3a3e9e06 1519 struct cpufreq_policy *policy;
42d4dc3f 1520
2d06d8c4 1521 pr_debug("suspending cpu %u\n", cpu);
42d4dc3f 1522
e00e56df 1523 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1524 policy = cpufreq_cpu_get(cpu);
1525 if (!policy)
e00e56df 1526 return 0;
42d4dc3f 1527
1c3d85dd 1528 if (cpufreq_driver->suspend) {
3a3e9e06 1529 ret = cpufreq_driver->suspend(policy);
ce6c3997 1530 if (ret)
42d4dc3f 1531 printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
3a3e9e06 1532 "step on CPU %u\n", policy->cpu);
42d4dc3f
BH
1533 }
1534
3a3e9e06 1535 cpufreq_cpu_put(policy);
c9060494 1536 return ret;
42d4dc3f
BH
1537}
1538
1da177e4 1539/**
e00e56df 1540 * cpufreq_bp_resume - Restore proper frequency handling of the boot CPU.
1da177e4
LT
1541 *
1542 * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
ce6c3997
DB
1543 * 2.) schedule call cpufreq_update_policy() ASAP as interrupts are
1544 * restored. It will verify that the current freq is in sync with
1545 * what we believe it to be. This is a bit later than when it
1546 * should be, but nonethteless it's better than calling
1547 * cpufreq_driver->get() here which might re-enable interrupts...
e00e56df
RW
1548 *
1549 * This function is only executed for the boot CPU. The other CPUs have not
1550 * been turned on yet.
1da177e4 1551 */
e00e56df 1552static void cpufreq_bp_resume(void)
1da177e4 1553{
e08f5f5b 1554 int ret = 0;
4bc5d341 1555
e00e56df 1556 int cpu = smp_processor_id();
3a3e9e06 1557 struct cpufreq_policy *policy;
1da177e4 1558
2d06d8c4 1559 pr_debug("resuming cpu %u\n", cpu);
1da177e4 1560
e00e56df 1561 /* If there's no policy for the boot CPU, we have nothing to do. */
3a3e9e06
VK
1562 policy = cpufreq_cpu_get(cpu);
1563 if (!policy)
e00e56df 1564 return;
1da177e4 1565
1c3d85dd 1566 if (cpufreq_driver->resume) {
3a3e9e06 1567 ret = cpufreq_driver->resume(policy);
1da177e4
LT
1568 if (ret) {
1569 printk(KERN_ERR "cpufreq: resume failed in ->resume "
3a3e9e06 1570 "step on CPU %u\n", policy->cpu);
c9060494 1571 goto fail;
1da177e4
LT
1572 }
1573 }
1574
3a3e9e06 1575 schedule_work(&policy->update);
ce6c3997 1576
c9060494 1577fail:
3a3e9e06 1578 cpufreq_cpu_put(policy);
1da177e4
LT
1579}
1580
e00e56df
RW
1581static struct syscore_ops cpufreq_syscore_ops = {
1582 .suspend = cpufreq_bp_suspend,
1583 .resume = cpufreq_bp_resume,
1da177e4
LT
1584};
1585
9d95046e
BP
1586/**
1587 * cpufreq_get_current_driver - return current driver's name
1588 *
1589 * Return the name string of the currently loaded cpufreq driver
1590 * or NULL, if none.
1591 */
1592const char *cpufreq_get_current_driver(void)
1593{
1c3d85dd
RW
1594 if (cpufreq_driver)
1595 return cpufreq_driver->name;
1596
1597 return NULL;
9d95046e
BP
1598}
1599EXPORT_SYMBOL_GPL(cpufreq_get_current_driver);
1da177e4
LT
1600
1601/*********************************************************************
1602 * NOTIFIER LISTS INTERFACE *
1603 *********************************************************************/
1604
1605/**
1606 * cpufreq_register_notifier - register a driver with cpufreq
1607 * @nb: notifier function to register
1608 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1609 *
32ee8c3e 1610 * Add a driver to one of two lists: either a list of drivers that
1da177e4
LT
1611 * are notified about clock rate changes (once before and once after
1612 * the transition), or a list of drivers that are notified about
1613 * changes in cpufreq policy.
1614 *
1615 * This function may sleep, and has the same return conditions as
e041c683 1616 * blocking_notifier_chain_register.
1da177e4
LT
1617 */
1618int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
1619{
1620 int ret;
1621
d5aaffa9
DB
1622 if (cpufreq_disabled())
1623 return -EINVAL;
1624
74212ca4
CEB
1625 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1626
1da177e4
LT
1627 switch (list) {
1628 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1629 ret = srcu_notifier_chain_register(
e041c683 1630 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1631 break;
1632 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1633 ret = blocking_notifier_chain_register(
1634 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1635 break;
1636 default:
1637 ret = -EINVAL;
1638 }
1da177e4
LT
1639
1640 return ret;
1641}
1642EXPORT_SYMBOL(cpufreq_register_notifier);
1643
1da177e4
LT
1644/**
1645 * cpufreq_unregister_notifier - unregister a driver with cpufreq
1646 * @nb: notifier block to be unregistered
bb176f7d 1647 * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
1da177e4
LT
1648 *
1649 * Remove a driver from the CPU frequency notifier list.
1650 *
1651 * This function may sleep, and has the same return conditions as
e041c683 1652 * blocking_notifier_chain_unregister.
1da177e4
LT
1653 */
1654int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
1655{
1656 int ret;
1657
d5aaffa9
DB
1658 if (cpufreq_disabled())
1659 return -EINVAL;
1660
1da177e4
LT
1661 switch (list) {
1662 case CPUFREQ_TRANSITION_NOTIFIER:
b4dfdbb3 1663 ret = srcu_notifier_chain_unregister(
e041c683 1664 &cpufreq_transition_notifier_list, nb);
1da177e4
LT
1665 break;
1666 case CPUFREQ_POLICY_NOTIFIER:
e041c683
AS
1667 ret = blocking_notifier_chain_unregister(
1668 &cpufreq_policy_notifier_list, nb);
1da177e4
LT
1669 break;
1670 default:
1671 ret = -EINVAL;
1672 }
1da177e4
LT
1673
1674 return ret;
1675}
1676EXPORT_SYMBOL(cpufreq_unregister_notifier);
1677
1678
1679/*********************************************************************
1680 * GOVERNORS *
1681 *********************************************************************/
1682
1da177e4
LT
1683int __cpufreq_driver_target(struct cpufreq_policy *policy,
1684 unsigned int target_freq,
1685 unsigned int relation)
1686{
1687 int retval = -EINVAL;
7249924e 1688 unsigned int old_target_freq = target_freq;
c32b6b8e 1689
a7b422cd
KRW
1690 if (cpufreq_disabled())
1691 return -ENODEV;
1692
7249924e
VK
1693 /* Make sure that target_freq is within supported range */
1694 if (target_freq > policy->max)
1695 target_freq = policy->max;
1696 if (target_freq < policy->min)
1697 target_freq = policy->min;
1698
1699 pr_debug("target for CPU %u: %u kHz, relation %u, requested %u kHz\n",
1700 policy->cpu, target_freq, relation, old_target_freq);
5a1c0228 1701
9c0ebcf7
VK
1702 /*
1703 * This might look like a redundant call as we are checking it again
1704 * after finding index. But it is left intentionally for cases where
1705 * exactly same freq is called again and so we can save on few function
1706 * calls.
1707 */
5a1c0228
VK
1708 if (target_freq == policy->cur)
1709 return 0;
1710
1c3d85dd
RW
1711 if (cpufreq_driver->target)
1712 retval = cpufreq_driver->target(policy, target_freq, relation);
9c0ebcf7
VK
1713 else if (cpufreq_driver->target_index) {
1714 struct cpufreq_frequency_table *freq_table;
1715 int index;
90d45d17 1716
9c0ebcf7
VK
1717 freq_table = cpufreq_frequency_get_table(policy->cpu);
1718 if (unlikely(!freq_table)) {
1719 pr_err("%s: Unable to find freq_table\n", __func__);
1720 goto out;
1721 }
1722
1723 retval = cpufreq_frequency_table_target(policy, freq_table,
1724 target_freq, relation, &index);
1725 if (unlikely(retval)) {
1726 pr_err("%s: Unable to find matching freq\n", __func__);
1727 goto out;
1728 }
1729
1730 if (freq_table[index].frequency == policy->cur)
1731 retval = 0;
1732 else
1733 retval = cpufreq_driver->target_index(policy, index);
1734 }
1735
1736out:
1da177e4
LT
1737 return retval;
1738}
1739EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
1740
1da177e4
LT
1741int cpufreq_driver_target(struct cpufreq_policy *policy,
1742 unsigned int target_freq,
1743 unsigned int relation)
1744{
f1829e4a 1745 int ret = -EINVAL;
1da177e4 1746
1b750e3b 1747 lock_policy_rwsem_write(policy->cpu);
1da177e4
LT
1748
1749 ret = __cpufreq_driver_target(policy, target_freq, relation);
1750
5a01f2e8 1751 unlock_policy_rwsem_write(policy->cpu);
1da177e4 1752
1da177e4
LT
1753 return ret;
1754}
1755EXPORT_SYMBOL_GPL(cpufreq_driver_target);
1756
153d7f3f 1757/*
153d7f3f
AV
1758 * when "event" is CPUFREQ_GOV_LIMITS
1759 */
1da177e4 1760
e08f5f5b
GS
1761static int __cpufreq_governor(struct cpufreq_policy *policy,
1762 unsigned int event)
1da177e4 1763{
cc993cab 1764 int ret;
6afde10c
TR
1765
1766 /* Only must be defined when default governor is known to have latency
1767 restrictions, like e.g. conservative or ondemand.
1768 That this is the case is already ensured in Kconfig
1769 */
1770#ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1771 struct cpufreq_governor *gov = &cpufreq_gov_performance;
1772#else
1773 struct cpufreq_governor *gov = NULL;
1774#endif
1c256245
TR
1775
1776 if (policy->governor->max_transition_latency &&
1777 policy->cpuinfo.transition_latency >
1778 policy->governor->max_transition_latency) {
6afde10c
TR
1779 if (!gov)
1780 return -EINVAL;
1781 else {
1782 printk(KERN_WARNING "%s governor failed, too long"
1783 " transition latency of HW, fallback"
1784 " to %s governor\n",
1785 policy->governor->name,
1786 gov->name);
1787 policy->governor = gov;
1788 }
1c256245 1789 }
1da177e4 1790
fe492f3f
VK
1791 if (event == CPUFREQ_GOV_POLICY_INIT)
1792 if (!try_module_get(policy->governor->owner))
1793 return -EINVAL;
1da177e4 1794
2d06d8c4 1795 pr_debug("__cpufreq_governor for CPU %u, event %u\n",
e08f5f5b 1796 policy->cpu, event);
95731ebb
XC
1797
1798 mutex_lock(&cpufreq_governor_lock);
56d07db2 1799 if ((policy->governor_enabled && event == CPUFREQ_GOV_START)
f73d3933
VK
1800 || (!policy->governor_enabled
1801 && (event == CPUFREQ_GOV_LIMITS || event == CPUFREQ_GOV_STOP))) {
95731ebb
XC
1802 mutex_unlock(&cpufreq_governor_lock);
1803 return -EBUSY;
1804 }
1805
1806 if (event == CPUFREQ_GOV_STOP)
1807 policy->governor_enabled = false;
1808 else if (event == CPUFREQ_GOV_START)
1809 policy->governor_enabled = true;
1810
1811 mutex_unlock(&cpufreq_governor_lock);
1812
1da177e4
LT
1813 ret = policy->governor->governor(policy, event);
1814
4d5dcc42
VK
1815 if (!ret) {
1816 if (event == CPUFREQ_GOV_POLICY_INIT)
1817 policy->governor->initialized++;
1818 else if (event == CPUFREQ_GOV_POLICY_EXIT)
1819 policy->governor->initialized--;
95731ebb
XC
1820 } else {
1821 /* Restore original values */
1822 mutex_lock(&cpufreq_governor_lock);
1823 if (event == CPUFREQ_GOV_STOP)
1824 policy->governor_enabled = true;
1825 else if (event == CPUFREQ_GOV_START)
1826 policy->governor_enabled = false;
1827 mutex_unlock(&cpufreq_governor_lock);
4d5dcc42 1828 }
b394058f 1829
fe492f3f
VK
1830 if (((event == CPUFREQ_GOV_POLICY_INIT) && ret) ||
1831 ((event == CPUFREQ_GOV_POLICY_EXIT) && !ret))
1da177e4
LT
1832 module_put(policy->governor->owner);
1833
1834 return ret;
1835}
1836
1da177e4
LT
1837int cpufreq_register_governor(struct cpufreq_governor *governor)
1838{
3bcb09a3 1839 int err;
1da177e4
LT
1840
1841 if (!governor)
1842 return -EINVAL;
1843
a7b422cd
KRW
1844 if (cpufreq_disabled())
1845 return -ENODEV;
1846
3fc54d37 1847 mutex_lock(&cpufreq_governor_mutex);
32ee8c3e 1848
b394058f 1849 governor->initialized = 0;
3bcb09a3
JF
1850 err = -EBUSY;
1851 if (__find_governor(governor->name) == NULL) {
1852 err = 0;
1853 list_add(&governor->governor_list, &cpufreq_governor_list);
1da177e4 1854 }
1da177e4 1855
32ee8c3e 1856 mutex_unlock(&cpufreq_governor_mutex);
3bcb09a3 1857 return err;
1da177e4
LT
1858}
1859EXPORT_SYMBOL_GPL(cpufreq_register_governor);
1860
1da177e4
LT
1861void cpufreq_unregister_governor(struct cpufreq_governor *governor)
1862{
90e41bac
PB
1863#ifdef CONFIG_HOTPLUG_CPU
1864 int cpu;
1865#endif
1866
1da177e4
LT
1867 if (!governor)
1868 return;
1869
a7b422cd
KRW
1870 if (cpufreq_disabled())
1871 return;
1872
90e41bac
PB
1873#ifdef CONFIG_HOTPLUG_CPU
1874 for_each_present_cpu(cpu) {
1875 if (cpu_online(cpu))
1876 continue;
1877 if (!strcmp(per_cpu(cpufreq_cpu_governor, cpu), governor->name))
1878 strcpy(per_cpu(cpufreq_cpu_governor, cpu), "\0");
1879 }
1880#endif
1881
3fc54d37 1882 mutex_lock(&cpufreq_governor_mutex);
1da177e4 1883 list_del(&governor->governor_list);
3fc54d37 1884 mutex_unlock(&cpufreq_governor_mutex);
1da177e4
LT
1885 return;
1886}
1887EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
1888
1889
1da177e4
LT
1890/*********************************************************************
1891 * POLICY INTERFACE *
1892 *********************************************************************/
1893
1894/**
1895 * cpufreq_get_policy - get the current cpufreq_policy
29464f28
DJ
1896 * @policy: struct cpufreq_policy into which the current cpufreq_policy
1897 * is written
1da177e4
LT
1898 *
1899 * Reads the current cpufreq policy.
1900 */
1901int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
1902{
1903 struct cpufreq_policy *cpu_policy;
1904 if (!policy)
1905 return -EINVAL;
1906
1907 cpu_policy = cpufreq_cpu_get(cpu);
1908 if (!cpu_policy)
1909 return -EINVAL;
1910
d5b73cd8 1911 memcpy(policy, cpu_policy, sizeof(*policy));
1da177e4
LT
1912
1913 cpufreq_cpu_put(cpu_policy);
1da177e4
LT
1914 return 0;
1915}
1916EXPORT_SYMBOL(cpufreq_get_policy);
1917
153d7f3f 1918/*
037ce839
VK
1919 * policy : current policy.
1920 * new_policy: policy to be set.
153d7f3f 1921 */
037ce839 1922static int cpufreq_set_policy(struct cpufreq_policy *policy,
3a3e9e06 1923 struct cpufreq_policy *new_policy)
1da177e4 1924{
7bd353a9 1925 int ret = 0, failed = 1;
1da177e4 1926
3a3e9e06
VK
1927 pr_debug("setting new policy for CPU %u: %u - %u kHz\n", new_policy->cpu,
1928 new_policy->min, new_policy->max);
1da177e4 1929
d5b73cd8 1930 memcpy(&new_policy->cpuinfo, &policy->cpuinfo, sizeof(policy->cpuinfo));
1da177e4 1931
3a3e9e06 1932 if (new_policy->min > policy->max || new_policy->max < policy->min) {
9c9a43ed
MD
1933 ret = -EINVAL;
1934 goto error_out;
1935 }
1936
1da177e4 1937 /* verify the cpu speed can be set within this limit */
3a3e9e06 1938 ret = cpufreq_driver->verify(new_policy);
1da177e4
LT
1939 if (ret)
1940 goto error_out;
1941
1da177e4 1942 /* adjust if necessary - all reasons */
e041c683 1943 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1944 CPUFREQ_ADJUST, new_policy);
1da177e4
LT
1945
1946 /* adjust if necessary - hardware incompatibility*/
e041c683 1947 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1948 CPUFREQ_INCOMPATIBLE, new_policy);
1da177e4 1949
bb176f7d
VK
1950 /*
1951 * verify the cpu speed can be set within this limit, which might be
1952 * different to the first one
1953 */
3a3e9e06 1954 ret = cpufreq_driver->verify(new_policy);
e041c683 1955 if (ret)
1da177e4 1956 goto error_out;
1da177e4
LT
1957
1958 /* notification of the new policy */
e041c683 1959 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
3a3e9e06 1960 CPUFREQ_NOTIFY, new_policy);
1da177e4 1961
3a3e9e06
VK
1962 policy->min = new_policy->min;
1963 policy->max = new_policy->max;
1da177e4 1964
2d06d8c4 1965 pr_debug("new min and max freqs are %u - %u kHz\n",
3a3e9e06 1966 policy->min, policy->max);
1da177e4 1967
1c3d85dd 1968 if (cpufreq_driver->setpolicy) {
3a3e9e06 1969 policy->policy = new_policy->policy;
2d06d8c4 1970 pr_debug("setting range\n");
3a3e9e06 1971 ret = cpufreq_driver->setpolicy(new_policy);
1da177e4 1972 } else {
3a3e9e06 1973 if (new_policy->governor != policy->governor) {
1da177e4 1974 /* save old, working values */
3a3e9e06 1975 struct cpufreq_governor *old_gov = policy->governor;
1da177e4 1976
2d06d8c4 1977 pr_debug("governor switch\n");
1da177e4
LT
1978
1979 /* end old governor */
3a3e9e06
VK
1980 if (policy->governor) {
1981 __cpufreq_governor(policy, CPUFREQ_GOV_STOP);
1982 unlock_policy_rwsem_write(new_policy->cpu);
1983 __cpufreq_governor(policy,
7bd353a9 1984 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1985 lock_policy_rwsem_write(new_policy->cpu);
7bd353a9 1986 }
1da177e4
LT
1987
1988 /* start new governor */
3a3e9e06
VK
1989 policy->governor = new_policy->governor;
1990 if (!__cpufreq_governor(policy, CPUFREQ_GOV_POLICY_INIT)) {
1991 if (!__cpufreq_governor(policy, CPUFREQ_GOV_START)) {
7bd353a9 1992 failed = 0;
955ef483 1993 } else {
3a3e9e06
VK
1994 unlock_policy_rwsem_write(new_policy->cpu);
1995 __cpufreq_governor(policy,
7bd353a9 1996 CPUFREQ_GOV_POLICY_EXIT);
3a3e9e06 1997 lock_policy_rwsem_write(new_policy->cpu);
955ef483 1998 }
7bd353a9
VK
1999 }
2000
2001 if (failed) {
1da177e4 2002 /* new governor failed, so re-start old one */
2d06d8c4 2003 pr_debug("starting governor %s failed\n",
3a3e9e06 2004 policy->governor->name);
1da177e4 2005 if (old_gov) {
3a3e9e06
VK
2006 policy->governor = old_gov;
2007 __cpufreq_governor(policy,
7bd353a9 2008 CPUFREQ_GOV_POLICY_INIT);
3a3e9e06 2009 __cpufreq_governor(policy,
e08f5f5b 2010 CPUFREQ_GOV_START);
1da177e4
LT
2011 }
2012 ret = -EINVAL;
2013 goto error_out;
2014 }
2015 /* might be a policy change, too, so fall through */
2016 }
2d06d8c4 2017 pr_debug("governor: change or update limits\n");
3de9bdeb 2018 ret = __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
1da177e4
LT
2019 }
2020
7d5e350f 2021error_out:
1da177e4
LT
2022 return ret;
2023}
2024
1da177e4
LT
2025/**
2026 * cpufreq_update_policy - re-evaluate an existing cpufreq policy
2027 * @cpu: CPU which shall be re-evaluated
2028 *
25985edc 2029 * Useful for policy notifiers which have different necessities
1da177e4
LT
2030 * at different times.
2031 */
2032int cpufreq_update_policy(unsigned int cpu)
2033{
3a3e9e06
VK
2034 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
2035 struct cpufreq_policy new_policy;
f1829e4a 2036 int ret;
1da177e4 2037
3a3e9e06 2038 if (!policy) {
f1829e4a
JL
2039 ret = -ENODEV;
2040 goto no_policy;
2041 }
1da177e4 2042
1b750e3b 2043 lock_policy_rwsem_write(cpu);
1da177e4 2044
2d06d8c4 2045 pr_debug("updating policy for CPU %u\n", cpu);
d5b73cd8 2046 memcpy(&new_policy, policy, sizeof(*policy));
3a3e9e06
VK
2047 new_policy.min = policy->user_policy.min;
2048 new_policy.max = policy->user_policy.max;
2049 new_policy.policy = policy->user_policy.policy;
2050 new_policy.governor = policy->user_policy.governor;
1da177e4 2051
bb176f7d
VK
2052 /*
2053 * BIOS might change freq behind our back
2054 * -> ask driver for current freq and notify governors about a change
2055 */
1c3d85dd 2056 if (cpufreq_driver->get) {
3a3e9e06
VK
2057 new_policy.cur = cpufreq_driver->get(cpu);
2058 if (!policy->cur) {
2d06d8c4 2059 pr_debug("Driver did not initialize current freq");
3a3e9e06 2060 policy->cur = new_policy.cur;
a85f7bd3 2061 } else {
9c0ebcf7 2062 if (policy->cur != new_policy.cur && has_target())
3a3e9e06
VK
2063 cpufreq_out_of_sync(cpu, policy->cur,
2064 new_policy.cur);
a85f7bd3 2065 }
0961dd0d
TR
2066 }
2067
037ce839 2068 ret = cpufreq_set_policy(policy, &new_policy);
1da177e4 2069
5a01f2e8
VP
2070 unlock_policy_rwsem_write(cpu);
2071
3a3e9e06 2072 cpufreq_cpu_put(policy);
f1829e4a 2073no_policy:
1da177e4
LT
2074 return ret;
2075}
2076EXPORT_SYMBOL(cpufreq_update_policy);
2077
2760984f 2078static int cpufreq_cpu_callback(struct notifier_block *nfb,
c32b6b8e
AR
2079 unsigned long action, void *hcpu)
2080{
2081 unsigned int cpu = (unsigned long)hcpu;
8a25a2fd 2082 struct device *dev;
5302c3fb 2083 bool frozen = false;
c32b6b8e 2084
8a25a2fd
KS
2085 dev = get_cpu_device(cpu);
2086 if (dev) {
5302c3fb
SB
2087
2088 if (action & CPU_TASKS_FROZEN)
2089 frozen = true;
2090
2091 switch (action & ~CPU_TASKS_FROZEN) {
c32b6b8e 2092 case CPU_ONLINE:
5302c3fb 2093 __cpufreq_add_dev(dev, NULL, frozen);
23d32899 2094 cpufreq_update_policy(cpu);
c32b6b8e 2095 break;
5302c3fb 2096
c32b6b8e 2097 case CPU_DOWN_PREPARE:
cedb70af 2098 __cpufreq_remove_dev_prepare(dev, NULL, frozen);
1aee40ac
SB
2099 break;
2100
2101 case CPU_POST_DEAD:
cedb70af 2102 __cpufreq_remove_dev_finish(dev, NULL, frozen);
c32b6b8e 2103 break;
5302c3fb 2104
5a01f2e8 2105 case CPU_DOWN_FAILED:
5302c3fb 2106 __cpufreq_add_dev(dev, NULL, frozen);
c32b6b8e
AR
2107 break;
2108 }
2109 }
2110 return NOTIFY_OK;
2111}
2112
9c36f746 2113static struct notifier_block __refdata cpufreq_cpu_notifier = {
bb176f7d 2114 .notifier_call = cpufreq_cpu_callback,
c32b6b8e 2115};
1da177e4
LT
2116
2117/*********************************************************************
2118 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2119 *********************************************************************/
2120
2121/**
2122 * cpufreq_register_driver - register a CPU Frequency driver
2123 * @driver_data: A struct cpufreq_driver containing the values#
2124 * submitted by the CPU Frequency driver.
2125 *
bb176f7d 2126 * Registers a CPU Frequency driver to this core code. This code
1da177e4 2127 * returns zero on success, -EBUSY when another driver got here first
32ee8c3e 2128 * (and isn't unregistered in the meantime).
1da177e4
LT
2129 *
2130 */
221dee28 2131int cpufreq_register_driver(struct cpufreq_driver *driver_data)
1da177e4
LT
2132{
2133 unsigned long flags;
2134 int ret;
2135
a7b422cd
KRW
2136 if (cpufreq_disabled())
2137 return -ENODEV;
2138
1da177e4 2139 if (!driver_data || !driver_data->verify || !driver_data->init ||
9c0ebcf7
VK
2140 !(driver_data->setpolicy || driver_data->target_index ||
2141 driver_data->target))
1da177e4
LT
2142 return -EINVAL;
2143
2d06d8c4 2144 pr_debug("trying to register driver %s\n", driver_data->name);
1da177e4
LT
2145
2146 if (driver_data->setpolicy)
2147 driver_data->flags |= CPUFREQ_CONST_LOOPS;
2148
0d1857a1 2149 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2150 if (cpufreq_driver) {
0d1857a1 2151 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4dea5806 2152 return -EEXIST;
1da177e4 2153 }
1c3d85dd 2154 cpufreq_driver = driver_data;
0d1857a1 2155 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1da177e4 2156
8a25a2fd 2157 ret = subsys_interface_register(&cpufreq_interface);
8f5bc2ab
JS
2158 if (ret)
2159 goto err_null_driver;
1da177e4 2160
1c3d85dd 2161 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
1da177e4
LT
2162 int i;
2163 ret = -ENODEV;
2164
2165 /* check for at least one working CPU */
7a6aedfa
MT
2166 for (i = 0; i < nr_cpu_ids; i++)
2167 if (cpu_possible(i) && per_cpu(cpufreq_cpu_data, i)) {
1da177e4 2168 ret = 0;
7a6aedfa
MT
2169 break;
2170 }
1da177e4
LT
2171
2172 /* if all ->init() calls failed, unregister */
2173 if (ret) {
2d06d8c4 2174 pr_debug("no CPU initialized for driver %s\n",
e08f5f5b 2175 driver_data->name);
8a25a2fd 2176 goto err_if_unreg;
1da177e4
LT
2177 }
2178 }
2179
8f5bc2ab 2180 register_hotcpu_notifier(&cpufreq_cpu_notifier);
2d06d8c4 2181 pr_debug("driver %s up and running\n", driver_data->name);
1da177e4 2182
8f5bc2ab 2183 return 0;
8a25a2fd
KS
2184err_if_unreg:
2185 subsys_interface_unregister(&cpufreq_interface);
8f5bc2ab 2186err_null_driver:
0d1857a1 2187 write_lock_irqsave(&cpufreq_driver_lock, flags);
1c3d85dd 2188 cpufreq_driver = NULL;
0d1857a1 2189 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
4d34a67d 2190 return ret;
1da177e4
LT
2191}
2192EXPORT_SYMBOL_GPL(cpufreq_register_driver);
2193
1da177e4
LT
2194/**
2195 * cpufreq_unregister_driver - unregister the current CPUFreq driver
2196 *
bb176f7d 2197 * Unregister the current CPUFreq driver. Only call this if you have
1da177e4
LT
2198 * the right to do so, i.e. if you have succeeded in initialising before!
2199 * Returns zero if successful, and -EINVAL if the cpufreq_driver is
2200 * currently not initialised.
2201 */
221dee28 2202int cpufreq_unregister_driver(struct cpufreq_driver *driver)
1da177e4
LT
2203{
2204 unsigned long flags;
2205
1c3d85dd 2206 if (!cpufreq_driver || (driver != cpufreq_driver))
1da177e4 2207 return -EINVAL;
1da177e4 2208
2d06d8c4 2209 pr_debug("unregistering driver %s\n", driver->name);
1da177e4 2210
8a25a2fd 2211 subsys_interface_unregister(&cpufreq_interface);
65edc68c 2212 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
1da177e4 2213
6eed9404 2214 down_write(&cpufreq_rwsem);
0d1857a1 2215 write_lock_irqsave(&cpufreq_driver_lock, flags);
6eed9404 2216
1c3d85dd 2217 cpufreq_driver = NULL;
6eed9404 2218
0d1857a1 2219 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
6eed9404 2220 up_write(&cpufreq_rwsem);
1da177e4
LT
2221
2222 return 0;
2223}
2224EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
5a01f2e8
VP
2225
2226static int __init cpufreq_core_init(void)
2227{
2228 int cpu;
2229
a7b422cd
KRW
2230 if (cpufreq_disabled())
2231 return -ENODEV;
2232
474deff7 2233 for_each_possible_cpu(cpu)
5a01f2e8 2234 init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
8aa84ad8 2235
2361be23 2236 cpufreq_global_kobject = kobject_create();
8aa84ad8 2237 BUG_ON(!cpufreq_global_kobject);
e00e56df 2238 register_syscore_ops(&cpufreq_syscore_ops);
8aa84ad8 2239
5a01f2e8
VP
2240 return 0;
2241}
5a01f2e8 2242core_initcall(cpufreq_core_init);
This page took 0.790272 seconds and 5 git commands to generate.