aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/cpufreq/cpufreq.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/cpufreq/cpufreq.c')
-rw-r--r--drivers/cpufreq/cpufreq.c262
1 files changed, 218 insertions, 44 deletions
diff --git a/drivers/cpufreq/cpufreq.c b/drivers/cpufreq/cpufreq.c
index 8d19f7c06010..199b52b7c3e1 100644
--- a/drivers/cpufreq/cpufreq.c
+++ b/drivers/cpufreq/cpufreq.c
@@ -39,7 +39,7 @@ static struct cpufreq_driver *cpufreq_driver;
39static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data); 39static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data);
40static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback); 40static DEFINE_PER_CPU(struct cpufreq_policy *, cpufreq_cpu_data_fallback);
41static DEFINE_RWLOCK(cpufreq_driver_lock); 41static DEFINE_RWLOCK(cpufreq_driver_lock);
42static DEFINE_MUTEX(cpufreq_governor_lock); 42DEFINE_MUTEX(cpufreq_governor_lock);
43static LIST_HEAD(cpufreq_policy_list); 43static LIST_HEAD(cpufreq_policy_list);
44 44
45#ifdef CONFIG_HOTPLUG_CPU 45#ifdef CONFIG_HOTPLUG_CPU
@@ -176,6 +176,20 @@ int cpufreq_generic_init(struct cpufreq_policy *policy,
176} 176}
177EXPORT_SYMBOL_GPL(cpufreq_generic_init); 177EXPORT_SYMBOL_GPL(cpufreq_generic_init);
178 178
179unsigned int cpufreq_generic_get(unsigned int cpu)
180{
181 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu);
182
183 if (!policy || IS_ERR(policy->clk)) {
184 pr_err("%s: No %s associated to cpu: %d\n", __func__,
185 policy ? "clk" : "policy", cpu);
186 return 0;
187 }
188
189 return clk_get_rate(policy->clk) / 1000;
190}
191EXPORT_SYMBOL_GPL(cpufreq_generic_get);
192
179struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu) 193struct cpufreq_policy *cpufreq_cpu_get(unsigned int cpu)
180{ 194{
181 struct cpufreq_policy *policy = NULL; 195 struct cpufreq_policy *policy = NULL;
@@ -320,10 +334,51 @@ void cpufreq_notify_transition(struct cpufreq_policy *policy,
320} 334}
321EXPORT_SYMBOL_GPL(cpufreq_notify_transition); 335EXPORT_SYMBOL_GPL(cpufreq_notify_transition);
322 336
337/* Do post notifications when there are chances that transition has failed */
338void cpufreq_notify_post_transition(struct cpufreq_policy *policy,
339 struct cpufreq_freqs *freqs, int transition_failed)
340{
341 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
342 if (!transition_failed)
343 return;
344
345 swap(freqs->old, freqs->new);
346 cpufreq_notify_transition(policy, freqs, CPUFREQ_PRECHANGE);
347 cpufreq_notify_transition(policy, freqs, CPUFREQ_POSTCHANGE);
348}
349EXPORT_SYMBOL_GPL(cpufreq_notify_post_transition);
350
323 351
324/********************************************************************* 352/*********************************************************************
325 * SYSFS INTERFACE * 353 * SYSFS INTERFACE *
326 *********************************************************************/ 354 *********************************************************************/
355ssize_t show_boost(struct kobject *kobj,
356 struct attribute *attr, char *buf)
357{
358 return sprintf(buf, "%d\n", cpufreq_driver->boost_enabled);
359}
360
361static ssize_t store_boost(struct kobject *kobj, struct attribute *attr,
362 const char *buf, size_t count)
363{
364 int ret, enable;
365
366 ret = sscanf(buf, "%d", &enable);
367 if (ret != 1 || enable < 0 || enable > 1)
368 return -EINVAL;
369
370 if (cpufreq_boost_trigger_state(enable)) {
371 pr_err("%s: Cannot %s BOOST!\n", __func__,
372 enable ? "enable" : "disable");
373 return -EINVAL;
374 }
375
376 pr_debug("%s: cpufreq BOOST %s\n", __func__,
377 enable ? "enabled" : "disabled");
378
379 return count;
380}
381define_one_global_rw(boost);
327 382
328static struct cpufreq_governor *__find_governor(const char *str_governor) 383static struct cpufreq_governor *__find_governor(const char *str_governor)
329{ 384{
@@ -929,6 +984,9 @@ static void cpufreq_policy_put_kobj(struct cpufreq_policy *policy)
929 struct kobject *kobj; 984 struct kobject *kobj;
930 struct completion *cmp; 985 struct completion *cmp;
931 986
987 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
988 CPUFREQ_REMOVE_POLICY, policy);
989
932 down_read(&policy->rwsem); 990 down_read(&policy->rwsem);
933 kobj = &policy->kobj; 991 kobj = &policy->kobj;
934 cmp = &policy->kobj_unregister; 992 cmp = &policy->kobj_unregister;
@@ -1051,14 +1109,6 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1051 goto err_set_policy_cpu; 1109 goto err_set_policy_cpu;
1052 } 1110 }
1053 1111
1054 if (cpufreq_driver->get) {
1055 policy->cur = cpufreq_driver->get(policy->cpu);
1056 if (!policy->cur) {
1057 pr_err("%s: ->get() failed\n", __func__);
1058 goto err_get_freq;
1059 }
1060 }
1061
1062 /* related cpus should atleast have policy->cpus */ 1112 /* related cpus should atleast have policy->cpus */
1063 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus); 1113 cpumask_or(policy->related_cpus, policy->related_cpus, policy->cpus);
1064 1114
@@ -1073,6 +1123,60 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1073 policy->user_policy.max = policy->max; 1123 policy->user_policy.max = policy->max;
1074 } 1124 }
1075 1125
1126 down_write(&policy->rwsem);
1127 write_lock_irqsave(&cpufreq_driver_lock, flags);
1128 for_each_cpu(j, policy->cpus)
1129 per_cpu(cpufreq_cpu_data, j) = policy;
1130 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1131
1132 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
1133 policy->cur = cpufreq_driver->get(policy->cpu);
1134 if (!policy->cur) {
1135 pr_err("%s: ->get() failed\n", __func__);
1136 goto err_get_freq;
1137 }
1138 }
1139
1140 /*
1141 * Sometimes boot loaders set CPU frequency to a value outside of
1142 * frequency table present with cpufreq core. In such cases CPU might be
1143 * unstable if it has to run on that frequency for long duration of time
1144 * and so its better to set it to a frequency which is specified in
1145 * freq-table. This also makes cpufreq stats inconsistent as
1146 * cpufreq-stats would fail to register because current frequency of CPU
1147 * isn't found in freq-table.
1148 *
1149 * Because we don't want this change to effect boot process badly, we go
1150 * for the next freq which is >= policy->cur ('cur' must be set by now,
1151 * otherwise we will end up setting freq to lowest of the table as 'cur'
1152 * is initialized to zero).
1153 *
1154 * We are passing target-freq as "policy->cur - 1" otherwise
1155 * __cpufreq_driver_target() would simply fail, as policy->cur will be
1156 * equal to target-freq.
1157 */
1158 if ((cpufreq_driver->flags & CPUFREQ_NEED_INITIAL_FREQ_CHECK)
1159 && has_target()) {
1160 /* Are we running at unknown frequency ? */
1161 ret = cpufreq_frequency_table_get_index(policy, policy->cur);
1162 if (ret == -EINVAL) {
1163 /* Warn user and fix it */
1164 pr_warn("%s: CPU%d: Running at unlisted freq: %u KHz\n",
1165 __func__, policy->cpu, policy->cur);
1166 ret = __cpufreq_driver_target(policy, policy->cur - 1,
1167 CPUFREQ_RELATION_L);
1168
1169 /*
1170 * Reaching here after boot in a few seconds may not
1171 * mean that system will remain stable at "unknown"
1172 * frequency for longer duration. Hence, a BUG_ON().
1173 */
1174 BUG_ON(ret);
1175 pr_warn("%s: CPU%d: Unlisted initial frequency changed to: %u KHz\n",
1176 __func__, policy->cpu, policy->cur);
1177 }
1178 }
1179
1076 blocking_notifier_call_chain(&cpufreq_policy_notifier_list, 1180 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1077 CPUFREQ_START, policy); 1181 CPUFREQ_START, policy);
1078 1182
@@ -1085,15 +1189,12 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1085 } 1189 }
1086#endif 1190#endif
1087 1191
1088 write_lock_irqsave(&cpufreq_driver_lock, flags);
1089 for_each_cpu(j, policy->cpus)
1090 per_cpu(cpufreq_cpu_data, j) = policy;
1091 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1092
1093 if (!frozen) { 1192 if (!frozen) {
1094 ret = cpufreq_add_dev_interface(policy, dev); 1193 ret = cpufreq_add_dev_interface(policy, dev);
1095 if (ret) 1194 if (ret)
1096 goto err_out_unregister; 1195 goto err_out_unregister;
1196 blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
1197 CPUFREQ_CREATE_POLICY, policy);
1097 } 1198 }
1098 1199
1099 write_lock_irqsave(&cpufreq_driver_lock, flags); 1200 write_lock_irqsave(&cpufreq_driver_lock, flags);
@@ -1106,6 +1207,7 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1106 policy->user_policy.policy = policy->policy; 1207 policy->user_policy.policy = policy->policy;
1107 policy->user_policy.governor = policy->governor; 1208 policy->user_policy.governor = policy->governor;
1108 } 1209 }
1210 up_write(&policy->rwsem);
1109 1211
1110 kobject_uevent(&policy->kobj, KOBJ_ADD); 1212 kobject_uevent(&policy->kobj, KOBJ_ADD);
1111 up_read(&cpufreq_rwsem); 1213 up_read(&cpufreq_rwsem);
@@ -1115,12 +1217,12 @@ static int __cpufreq_add_dev(struct device *dev, struct subsys_interface *sif,
1115 return 0; 1217 return 0;
1116 1218
1117err_out_unregister: 1219err_out_unregister:
1220err_get_freq:
1118 write_lock_irqsave(&cpufreq_driver_lock, flags); 1221 write_lock_irqsave(&cpufreq_driver_lock, flags);
1119 for_each_cpu(j, policy->cpus) 1222 for_each_cpu(j, policy->cpus)
1120 per_cpu(cpufreq_cpu_data, j) = NULL; 1223 per_cpu(cpufreq_cpu_data, j) = NULL;
1121 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 1224 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
1122 1225
1123err_get_freq:
1124 if (cpufreq_driver->exit) 1226 if (cpufreq_driver->exit)
1125 cpufreq_driver->exit(policy); 1227 cpufreq_driver->exit(policy);
1126err_set_policy_cpu: 1228err_set_policy_cpu:
@@ -1223,8 +1325,7 @@ static int __cpufreq_remove_dev_prepare(struct device *dev,
1223 up_read(&policy->rwsem); 1325 up_read(&policy->rwsem);
1224 1326
1225 if (cpu != policy->cpu) { 1327 if (cpu != policy->cpu) {
1226 if (!frozen) 1328 sysfs_remove_link(&dev->kobj, "cpufreq");
1227 sysfs_remove_link(&dev->kobj, "cpufreq");
1228 } else if (cpus > 1) { 1329 } else if (cpus > 1) {
1229 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu); 1330 new_cpu = cpufreq_nominate_new_policy_cpu(policy, cpu);
1230 if (new_cpu >= 0) { 1331 if (new_cpu >= 0) {
@@ -1447,23 +1548,16 @@ static unsigned int __cpufreq_get(unsigned int cpu)
1447 */ 1548 */
1448unsigned int cpufreq_get(unsigned int cpu) 1549unsigned int cpufreq_get(unsigned int cpu)
1449{ 1550{
1450 struct cpufreq_policy *policy = per_cpu(cpufreq_cpu_data, cpu); 1551 struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
1451 unsigned int ret_freq = 0; 1552 unsigned int ret_freq = 0;
1452 1553
1453 if (cpufreq_disabled() || !cpufreq_driver) 1554 if (policy) {
1454 return -ENOENT; 1555 down_read(&policy->rwsem);
1455 1556 ret_freq = __cpufreq_get(cpu);
1456 BUG_ON(!policy); 1557 up_read(&policy->rwsem);
1457
1458 if (!down_read_trylock(&cpufreq_rwsem))
1459 return 0;
1460
1461 down_read(&policy->rwsem);
1462
1463 ret_freq = __cpufreq_get(cpu);
1464 1558
1465 up_read(&policy->rwsem); 1559 cpufreq_cpu_put(policy);
1466 up_read(&cpufreq_rwsem); 1560 }
1467 1561
1468 return ret_freq; 1562 return ret_freq;
1469} 1563}
@@ -1725,17 +1819,8 @@ int __cpufreq_driver_target(struct cpufreq_policy *policy,
1725 pr_err("%s: Failed to change cpu frequency: %d\n", 1819 pr_err("%s: Failed to change cpu frequency: %d\n",
1726 __func__, retval); 1820 __func__, retval);
1727 1821
1728 if (notify) { 1822 if (notify)
1729 /* 1823 cpufreq_notify_post_transition(policy, &freqs, retval);
1730 * Notify with old freq in case we failed to change
1731 * frequency
1732 */
1733 if (retval)
1734 freqs.new = freqs.old;
1735
1736 cpufreq_notify_transition(policy, &freqs,
1737 CPUFREQ_POSTCHANGE);
1738 }
1739 } 1824 }
1740 1825
1741out: 1826out:
@@ -2058,7 +2143,7 @@ int cpufreq_update_policy(unsigned int cpu)
2058 * BIOS might change freq behind our back 2143 * BIOS might change freq behind our back
2059 * -> ask driver for current freq and notify governors about a change 2144 * -> ask driver for current freq and notify governors about a change
2060 */ 2145 */
2061 if (cpufreq_driver->get) { 2146 if (cpufreq_driver->get && !cpufreq_driver->setpolicy) {
2062 new_policy.cur = cpufreq_driver->get(cpu); 2147 new_policy.cur = cpufreq_driver->get(cpu);
2063 if (!policy->cur) { 2148 if (!policy->cur) {
2064 pr_debug("Driver did not initialize current freq"); 2149 pr_debug("Driver did not initialize current freq");
@@ -2120,6 +2205,73 @@ static struct notifier_block __refdata cpufreq_cpu_notifier = {
2120}; 2205};
2121 2206
2122/********************************************************************* 2207/*********************************************************************
2208 * BOOST *
2209 *********************************************************************/
2210static int cpufreq_boost_set_sw(int state)
2211{
2212 struct cpufreq_frequency_table *freq_table;
2213 struct cpufreq_policy *policy;
2214 int ret = -EINVAL;
2215
2216 list_for_each_entry(policy, &cpufreq_policy_list, policy_list) {
2217 freq_table = cpufreq_frequency_get_table(policy->cpu);
2218 if (freq_table) {
2219 ret = cpufreq_frequency_table_cpuinfo(policy,
2220 freq_table);
2221 if (ret) {
2222 pr_err("%s: Policy frequency update failed\n",
2223 __func__);
2224 break;
2225 }
2226 policy->user_policy.max = policy->max;
2227 __cpufreq_governor(policy, CPUFREQ_GOV_LIMITS);
2228 }
2229 }
2230
2231 return ret;
2232}
2233
2234int cpufreq_boost_trigger_state(int state)
2235{
2236 unsigned long flags;
2237 int ret = 0;
2238
2239 if (cpufreq_driver->boost_enabled == state)
2240 return 0;
2241
2242 write_lock_irqsave(&cpufreq_driver_lock, flags);
2243 cpufreq_driver->boost_enabled = state;
2244 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2245
2246 ret = cpufreq_driver->set_boost(state);
2247 if (ret) {
2248 write_lock_irqsave(&cpufreq_driver_lock, flags);
2249 cpufreq_driver->boost_enabled = !state;
2250 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2251
2252 pr_err("%s: Cannot %s BOOST\n", __func__,
2253 state ? "enable" : "disable");
2254 }
2255
2256 return ret;
2257}
2258
2259int cpufreq_boost_supported(void)
2260{
2261 if (likely(cpufreq_driver))
2262 return cpufreq_driver->boost_supported;
2263
2264 return 0;
2265}
2266EXPORT_SYMBOL_GPL(cpufreq_boost_supported);
2267
2268int cpufreq_boost_enabled(void)
2269{
2270 return cpufreq_driver->boost_enabled;
2271}
2272EXPORT_SYMBOL_GPL(cpufreq_boost_enabled);
2273
2274/*********************************************************************
2123 * REGISTER / UNREGISTER CPUFREQ DRIVER * 2275 * REGISTER / UNREGISTER CPUFREQ DRIVER *
2124 *********************************************************************/ 2276 *********************************************************************/
2125 2277
@@ -2159,9 +2311,25 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2159 cpufreq_driver = driver_data; 2311 cpufreq_driver = driver_data;
2160 write_unlock_irqrestore(&cpufreq_driver_lock, flags); 2312 write_unlock_irqrestore(&cpufreq_driver_lock, flags);
2161 2313
2314 if (cpufreq_boost_supported()) {
2315 /*
2316 * Check if driver provides function to enable boost -
2317 * if not, use cpufreq_boost_set_sw as default
2318 */
2319 if (!cpufreq_driver->set_boost)
2320 cpufreq_driver->set_boost = cpufreq_boost_set_sw;
2321
2322 ret = cpufreq_sysfs_create_file(&boost.attr);
2323 if (ret) {
2324 pr_err("%s: cannot register global BOOST sysfs file\n",
2325 __func__);
2326 goto err_null_driver;
2327 }
2328 }
2329
2162 ret = subsys_interface_register(&cpufreq_interface); 2330 ret = subsys_interface_register(&cpufreq_interface);
2163 if (ret) 2331 if (ret)
2164 goto err_null_driver; 2332 goto err_boost_unreg;
2165 2333
2166 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) { 2334 if (!(cpufreq_driver->flags & CPUFREQ_STICKY)) {
2167 int i; 2335 int i;
@@ -2188,6 +2356,9 @@ int cpufreq_register_driver(struct cpufreq_driver *driver_data)
2188 return 0; 2356 return 0;
2189err_if_unreg: 2357err_if_unreg:
2190 subsys_interface_unregister(&cpufreq_interface); 2358 subsys_interface_unregister(&cpufreq_interface);
2359err_boost_unreg:
2360 if (cpufreq_boost_supported())
2361 cpufreq_sysfs_remove_file(&boost.attr);
2191err_null_driver: 2362err_null_driver:
2192 write_lock_irqsave(&cpufreq_driver_lock, flags); 2363 write_lock_irqsave(&cpufreq_driver_lock, flags);
2193 cpufreq_driver = NULL; 2364 cpufreq_driver = NULL;
@@ -2214,6 +2385,9 @@ int cpufreq_unregister_driver(struct cpufreq_driver *driver)
2214 pr_debug("unregistering driver %s\n", driver->name); 2385 pr_debug("unregistering driver %s\n", driver->name);
2215 2386
2216 subsys_interface_unregister(&cpufreq_interface); 2387 subsys_interface_unregister(&cpufreq_interface);
2388 if (cpufreq_boost_supported())
2389 cpufreq_sysfs_remove_file(&boost.attr);
2390
2217 unregister_hotcpu_notifier(&cpufreq_cpu_notifier); 2391 unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
2218 2392
2219 down_write(&cpufreq_rwsem); 2393 down_write(&cpufreq_rwsem);