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