cpufreq.c 46 KB

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