18 #include <linux/kernel.h>
19 #include <linux/module.h>
26 #include <linux/device.h>
27 #include <linux/slab.h>
42 #ifdef CONFIG_HOTPLUG_CPU
70 #define lock_policy_rwsem(mode, cpu) \
71 static int lock_policy_rwsem_##mode \
74 int policy_cpu = per_cpu(cpufreq_policy_cpu, cpu); \
75 BUG_ON(policy_cpu == -1); \
76 down_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
77 if (unlikely(!cpu_online(cpu))) { \
78 up_##mode(&per_cpu(cpu_policy_rwsem, policy_cpu)); \
89 static void unlock_policy_rwsem_read(
int cpu)
91 int policy_cpu =
per_cpu(cpufreq_policy_cpu, cpu);
96 static void unlock_policy_rwsem_write(
int cpu)
98 int policy_cpu =
per_cpu(cpufreq_policy_cpu, cpu);
107 static unsigned int __cpufreq_get(
unsigned int cpu);
120 static bool init_cpufreq_transition_notifier_list_called;
121 static int __init init_cpufreq_transition_notifier_list(
void)
124 init_cpufreq_transition_notifier_list_called =
true;
141 static struct cpufreq_policy *__cpufreq_cpu_get(
unsigned int cpu,
bool sysfs)
146 if (cpu >= nr_cpu_ids)
155 if (!try_module_get(cpufreq_driver->
owner))
160 data =
per_cpu(cpufreq_cpu_data, cpu);
163 goto err_out_put_module;
166 goto err_out_put_module;
168 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
172 module_put(cpufreq_driver->
owner);
174 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
181 return __cpufreq_cpu_get(cpu,
false);
185 static struct cpufreq_policy *cpufreq_cpu_get_sysfs(
unsigned int cpu)
187 return __cpufreq_cpu_get(cpu,
true);
190 static void __cpufreq_cpu_put(
struct cpufreq_policy *data,
bool sysfs)
194 module_put(cpufreq_driver->
owner);
199 __cpufreq_cpu_put(data,
false);
205 __cpufreq_cpu_put(data,
true);
221 static unsigned long l_p_j_ref;
222 static unsigned int l_p_j_ref_freq;
229 if (!l_p_j_ref_freq) {
231 l_p_j_ref_freq = ci->
old;
232 pr_debug(
"saving %lu as reference value for loops_per_jiffy; "
233 "freq is %u kHz\n", l_p_j_ref, l_p_j_ref_freq);
239 pr_debug(
"scaling loops_per_jiffy to %lu "
244 static inline void adjust_jiffies(
unsigned long val,
struct cpufreq_freqs *ci)
266 pr_debug(
"notification %u of frequency transition to %u kHz\n",
269 policy =
per_cpu(cpufreq_cpu_data, freqs->
cpu);
278 if ((policy) && (policy->
cpu == freqs->
cpu) &&
279 (policy->
cur) && (policy->
cur != freqs->
old)) {
280 pr_debug(
"Warning: CPU frequency is"
281 " %u, cpufreq assumed %u kHz.\n",
293 pr_debug(
"FREQ: %lu - CPU: %lu", (
unsigned long)freqs->
new,
294 (
unsigned long)freqs->
cpu);
296 trace_cpu_frequency(freqs->
new, freqs->
cpu);
326 static
int cpufreq_parse_governor(
char *str_governor,
unsigned int *policy,
338 }
else if (!
strnicmp(str_governor,
"powersave",
343 }
else if (cpufreq_driver->
target) {
348 t = __find_governor(str_governor);
354 ret = request_module(
"cpufreq_%s", str_governor);
358 t = __find_governor(str_governor);
381 #define show_one(file_name, object) \
382 static ssize_t show_##file_name \
383 (struct cpufreq_policy *policy, char *buf) \
385 return sprintf(buf, "%u\n", policy->object); \
401 #define store_one(file_name, object) \
402 static ssize_t store_##file_name \
403 (struct cpufreq_policy *policy, const char *buf, size_t count) \
405 unsigned int ret = -EINVAL; \
406 struct cpufreq_policy new_policy; \
408 ret = cpufreq_get_policy(&new_policy, policy->cpu); \
412 ret = sscanf(buf, "%u", &new_policy.object); \
416 ret = __cpufreq_set_policy(policy, &new_policy); \
417 policy->user_policy.object = policy->object; \
419 return ret ? ret : count; \
431 unsigned int cur_freq = __cpufreq_get(policy->
cpu);
433 return sprintf(buf,
"<unknown>");
434 return sprintf(buf,
"%u\n", cur_freq);
444 return sprintf(buf,
"powersave\n");
446 return sprintf(buf,
"performance\n");
458 const char *buf,
size_t count)
461 char str_governor[16];
468 ret =
sscanf(buf,
"%15s", str_governor);
472 if (cpufreq_parse_governor(str_governor, &new_policy.policy,
473 &new_policy.governor))
478 ret = __cpufreq_set_policy(policy, &new_policy);
506 if (!cpufreq_driver->
target) {
507 i +=
sprintf(buf,
"performance powersave");
545 return show_cpus(policy->
cpus, buf);
554 return show_cpus(policy->
cpus, buf);
558 const char *buf,
size_t count)
560 unsigned int freq = 0;
566 ret =
sscanf(buf,
"%u", &freq);
570 policy->
governor->store_setspeed(policy, freq);
578 return sprintf(buf,
"<unsupported>\n");
580 return policy->
governor->show_setspeed(policy, buf);
593 return sprintf(buf,
"%u\n", limit);
613 static struct attribute *default_attrs[] = {
614 &cpuinfo_min_freq.attr,
615 &cpuinfo_max_freq.attr,
616 &cpuinfo_transition_latency.attr,
617 &scaling_min_freq.attr,
618 &scaling_max_freq.attr,
621 &scaling_governor.attr,
622 &scaling_driver.attr,
623 &scaling_available_governors.attr,
624 &scaling_setspeed.attr,
631 #define to_policy(k) container_of(k, struct cpufreq_policy, kobj)
632 #define to_attr(a) container_of(a, struct freq_attr, attr)
639 policy = cpufreq_cpu_get_sysfs(policy->
cpu);
643 if (lock_policy_rwsem_read(policy->
cpu) < 0)
647 ret = fattr->
show(policy, buf);
651 unlock_policy_rwsem_read(policy->
cpu);
653 cpufreq_cpu_put_sysfs(policy);
659 const char *buf,
size_t count)
664 policy = cpufreq_cpu_get_sysfs(policy->
cpu);
668 if (lock_policy_rwsem_write(policy->
cpu) < 0)
672 ret = fattr->
store(policy, buf, count);
676 unlock_policy_rwsem_write(policy->
cpu);
678 cpufreq_cpu_put_sysfs(policy);
683 static void cpufreq_sysfs_release(
struct kobject *kobj)
686 pr_debug(
"last reference is dropped\n");
695 static struct kobj_type ktype_cpufreq = {
698 .release = cpufreq_sysfs_release,
707 static int cpufreq_add_dev_policy(
unsigned int cpu,
715 #ifdef CONFIG_HOTPLUG_CPU
718 gov = __find_governor(
per_cpu(cpufreq_cpu_governor, cpu));
721 pr_debug(
"Restoring governor %s for cpu %d\n",
741 unlock_policy_rwsem_write(cpu);
742 per_cpu(cpufreq_policy_cpu, cpu) = managed_policy->
cpu;
744 if (lock_policy_rwsem_write(cpu) < 0) {
746 if (cpufreq_driver->
exit)
747 cpufreq_driver->
exit(policy);
753 cpumask_copy(managed_policy->
cpus, policy->
cpus);
754 per_cpu(cpufreq_cpu_data, cpu) = managed_policy;
755 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
757 pr_debug(
"CPU already managed, adding link\n");
759 &managed_policy->
kobj,
768 if (cpufreq_driver->
exit)
769 cpufreq_driver->
exit(policy);
783 static int cpufreq_add_dev_symlink(
unsigned int cpu,
798 pr_debug(
"CPU %u already managed, adding link\n", j);
811 static int cpufreq_add_dev_interface(
unsigned int cpu,
823 &dev->
kobj,
"cpufreq");
828 drv_attr = cpufreq_driver->
attr;
829 while ((drv_attr) && (*drv_attr)) {
832 goto err_out_kobj_put;
835 if (cpufreq_driver->
get) {
838 goto err_out_kobj_put;
840 if (cpufreq_driver->
target) {
843 goto err_out_kobj_put;
848 goto err_out_kobj_put;
855 per_cpu(cpufreq_cpu_data, j) = policy;
858 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
860 ret = cpufreq_add_dev_symlink(cpu, policy);
862 goto err_out_kobj_put;
869 ret = __cpufreq_set_policy(policy, &new_policy);
874 pr_debug(
"setting policy failed\n");
875 if (cpufreq_driver->
exit)
876 cpufreq_driver->
exit(policy);
898 unsigned int cpu = dev->
id;
899 int ret = 0, found = 0;
903 #ifdef CONFIG_HOTPLUG_CPU
922 if (!try_module_get(cpufreq_driver->
owner)) {
933 goto err_free_policy;
936 goto err_free_cpumask;
943 ret = (lock_policy_rwsem_write(cpu) < 0);
950 #ifdef CONFIG_HOTPLUG_CPU
962 policy->
governor = CPUFREQ_DEFAULT_GOVERNOR;
966 ret = cpufreq_driver->
init(policy);
968 pr_debug(
"initialization failed\n");
969 goto err_unlock_policy;
977 ret = cpufreq_add_dev_policy(cpu, policy, dev);
983 goto err_unlock_policy;
986 ret = cpufreq_add_dev_interface(cpu, policy, dev);
988 goto err_out_unregister;
990 unlock_policy_rwsem_write(cpu);
993 module_put(cpufreq_driver->
owner);
994 pr_debug(
"initialization complete\n");
1002 per_cpu(cpufreq_cpu_data, j) = NULL;
1003 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1009 unlock_policy_rwsem_write(cpu);
1012 free_cpumask_var(policy->
cpus);
1016 module_put(cpufreq_driver->
owner);
1031 unsigned int cpu = dev->id;
1032 unsigned long flags;
1041 pr_debug(
"unregistering CPU %u\n", cpu);
1044 data =
per_cpu(cpufreq_cpu_data, cpu);
1047 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1048 unlock_policy_rwsem_write(cpu);
1060 cpumask_clear_cpu(cpu, data->
cpus);
1061 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1064 unlock_policy_rwsem_write(cpu);
1072 #ifdef CONFIG_HOTPLUG_CPU
1090 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1096 pr_debug(
"removing link for cpu %u\n", j);
1097 #ifdef CONFIG_HOTPLUG_CPU
1102 kobj = &cpu_dev->
kobj;
1103 unlock_policy_rwsem_write(cpu);
1105 lock_policy_rwsem_write(cpu);
1110 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1113 if (cpufreq_driver->
target)
1118 unlock_policy_rwsem_write(cpu);
1125 pr_debug(
"waiting for dropping of refcount\n");
1129 lock_policy_rwsem_write(cpu);
1130 if (cpufreq_driver->
exit)
1131 cpufreq_driver->
exit(data);
1132 unlock_policy_rwsem_write(cpu);
1134 #ifdef CONFIG_HOTPLUG_CPU
1141 cpumask_clear_cpu(cpu, data->
cpus);
1145 lock_policy_rwsem_write(cpu);
1146 __cpufreq_remove_dev(dev, sif);
1151 free_cpumask_var(data->
cpus);
1160 unsigned int cpu = dev->
id;
1166 if (
unlikely(lock_policy_rwsem_write(cpu)))
1169 retval = __cpufreq_remove_dev(dev, sif);
1178 unsigned int cpu = policy->
cpu;
1179 pr_debug(
"handle_update for cpu %u called\n", cpu);
1192 static void cpufreq_out_of_sync(
unsigned int cpu,
unsigned int old_freq,
1193 unsigned int new_freq)
1197 pr_debug(
"Warning: CPU frequency out of sync: cpufreq and timing "
1198 "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
1201 freqs.old = old_freq;
1202 freqs.new = new_freq;
1218 unsigned int ret_freq = 0;
1221 ret_freq = policy->
cur;
1238 unsigned int ret_freq = 0;
1241 ret_freq = policy->
max;
1250 static unsigned int __cpufreq_get(
unsigned int cpu)
1253 unsigned int ret_freq = 0;
1255 if (!cpufreq_driver->
get)
1258 ret_freq = cpufreq_driver->
get(cpu);
1260 if (ret_freq && policy->
cur &&
1265 cpufreq_out_of_sync(cpu, policy->
cur, ret_freq);
1281 unsigned int ret_freq = 0;
1287 if (
unlikely(lock_policy_rwsem_read(cpu)))
1290 ret_freq = __cpufreq_get(cpu);
1292 unlock_policy_rwsem_read(cpu);
1304 .add_dev = cpufreq_add_dev,
1305 .remove_dev = cpufreq_remove_dev,
1315 static int cpufreq_bp_suspend(
void)
1322 pr_debug(
"suspending cpu %u\n", cpu);
1329 if (cpufreq_driver->
suspend) {
1330 ret = cpufreq_driver->
suspend(cpu_policy);
1333 "step on CPU %u\n", cpu_policy->
cpu);
1353 static void cpufreq_bp_resume(
void)
1360 pr_debug(
"resuming cpu %u\n", cpu);
1367 if (cpufreq_driver->
resume) {
1368 ret = cpufreq_driver->
resume(cpu_policy);
1371 "step on CPU %u\n", cpu_policy->
cpu);
1383 .suspend = cpufreq_bp_suspend,
1384 .resume = cpufreq_bp_resume,
1409 WARN_ON(!init_cpufreq_transition_notifier_list_called);
1414 &cpufreq_transition_notifier_list, nb);
1418 &cpufreq_policy_notifier_list, nb);
1446 &cpufreq_transition_notifier_list, nb);
1450 &cpufreq_policy_notifier_list, nb);
1467 unsigned int target_freq,
1468 unsigned int relation)
1475 pr_debug(
"target for CPU %u: %u kHz, relation %u\n", policy->
cpu,
1476 target_freq, relation);
1478 retval = cpufreq_driver->
target(policy, target_freq, relation);
1485 unsigned int target_freq,
1486 unsigned int relation)
1494 if (
unlikely(lock_policy_rwsem_write(policy->
cpu)))
1499 unlock_policy_rwsem_write(policy->
cpu);
1517 ret = cpufreq_driver->
getavg(policy, cpu);
1537 #ifdef CONFIG_CPU_FREQ_GOV_PERFORMANCE
1543 if (policy->
governor->max_transition_latency &&
1544 policy->
cpuinfo.transition_latency >
1545 policy->
governor->max_transition_latency) {
1550 " transition latency of HW, fallback"
1551 " to %s governor\n",
1558 if (!try_module_get(policy->
governor->owner))
1561 pr_debug(
"__cpufreq_governor for CPU %u, event %u\n",
1562 policy->
cpu, event);
1563 ret = policy->
governor->governor(policy, event);
1568 module_put(policy->
governor->owner);
1570 module_put(policy->
governor->owner);
1589 if (__find_governor(governor->
name) == NULL) {
1602 #ifdef CONFIG_HOTPLUG_CPU
1612 #ifdef CONFIG_HOTPLUG_CPU
1668 pr_debug(
"setting new policy for CPU %u: %u - %u kHz\n", policy->
cpu,
1669 policy->
min, policy->
max);
1674 if (policy->
min > data->
max || policy->
max < data->
min) {
1680 ret = cpufreq_driver->
verify(policy);
1694 ret = cpufreq_driver->
verify(policy);
1705 pr_debug(
"new min and max freqs are %u - %u kHz\n",
1711 ret = cpufreq_driver->
setpolicy(policy);
1727 pr_debug(
"starting governor %s failed\n",
1731 __cpufreq_governor(data,
1739 pr_debug(
"governor: change or update limits\n");
1765 if (
unlikely(lock_policy_rwsem_write(cpu))) {
1770 pr_debug(
"updating policy for CPU %u\n", cpu);
1779 if (cpufreq_driver->
get) {
1780 policy.
cur = cpufreq_driver->
get(cpu);
1782 pr_debug(
"Driver did not initialize current freq");
1785 if (data->
cur != policy.
cur)
1786 cpufreq_out_of_sync(cpu, data->
cur,
1791 ret = __cpufreq_set_policy(data, &policy);
1793 unlock_policy_rwsem_write(cpu);
1803 unsigned long action,
void *hcpu)
1805 unsigned int cpu = (
unsigned long)hcpu;
1813 cpufreq_add_dev(dev, NULL);
1817 if (
unlikely(lock_policy_rwsem_write(cpu)))
1820 __cpufreq_remove_dev(dev, NULL);
1824 cpufreq_add_dev(dev, NULL);
1832 .notifier_call = cpufreq_cpu_callback,
1851 unsigned long flags;
1857 if (!driver_data || !driver_data->
verify || !driver_data->
init ||
1861 pr_debug(
"trying to register driver %s\n", driver_data->
name);
1867 if (cpufreq_driver) {
1868 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1871 cpufreq_driver = driver_data;
1872 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1876 goto err_null_driver;
1891 pr_debug(
"no CPU initialized for driver %s\n",
1898 pr_debug(
"driver %s up and running\n", driver_data->
name);
1905 cpufreq_driver =
NULL;
1906 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1922 unsigned long flags;
1924 if (!cpufreq_driver || (driver != cpufreq_driver))
1933 cpufreq_driver =
NULL;
1934 spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
1940 static int __init cpufreq_core_init(
void)
1948 per_cpu(cpufreq_policy_cpu, cpu) = -1;
1953 BUG_ON(!cpufreq_global_kobject);