cpufreq.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  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. #ifdef CONFIG_SMP
  638. for_each_cpu_mask(j, policy->cpus) {
  639. if (cpu == j)
  640. continue;
  641. /* check for existing affected CPUs. They may not be aware
  642. * of it due to CPU Hotplug.
  643. */
  644. managed_policy = cpufreq_cpu_get(j);
  645. if (unlikely(managed_policy)) {
  646. /* Set proper policy_cpu */
  647. unlock_policy_rwsem_write(cpu);
  648. per_cpu(policy_cpu, cpu) = managed_policy->cpu;
  649. if (lock_policy_rwsem_write(cpu) < 0)
  650. goto err_out_driver_exit;
  651. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  652. managed_policy->cpus = policy->cpus;
  653. cpufreq_cpu_data[cpu] = managed_policy;
  654. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  655. dprintk("CPU already managed, adding link\n");
  656. ret = sysfs_create_link(&sys_dev->kobj,
  657. &managed_policy->kobj,
  658. "cpufreq");
  659. if (ret) {
  660. unlock_policy_rwsem_write(cpu);
  661. goto err_out_driver_exit;
  662. }
  663. cpufreq_debug_enable_ratelimit();
  664. ret = 0;
  665. unlock_policy_rwsem_write(cpu);
  666. goto err_out_driver_exit; /* call driver->exit() */
  667. }
  668. }
  669. #endif
  670. memcpy(&new_policy, policy, sizeof(struct cpufreq_policy));
  671. /* prepare interface data */
  672. policy->kobj.parent = &sys_dev->kobj;
  673. policy->kobj.ktype = &ktype_cpufreq;
  674. strlcpy(policy->kobj.name, "cpufreq", KOBJ_NAME_LEN);
  675. ret = kobject_register(&policy->kobj);
  676. if (ret) {
  677. unlock_policy_rwsem_write(cpu);
  678. goto err_out_driver_exit;
  679. }
  680. /* set up files for this cpu device */
  681. drv_attr = cpufreq_driver->attr;
  682. while ((drv_attr) && (*drv_attr)) {
  683. sysfs_create_file(&policy->kobj, &((*drv_attr)->attr));
  684. drv_attr++;
  685. }
  686. if (cpufreq_driver->get)
  687. sysfs_create_file(&policy->kobj, &cpuinfo_cur_freq.attr);
  688. if (cpufreq_driver->target)
  689. sysfs_create_file(&policy->kobj, &scaling_cur_freq.attr);
  690. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  691. for_each_cpu_mask(j, policy->cpus) {
  692. cpufreq_cpu_data[j] = policy;
  693. per_cpu(policy_cpu, j) = policy->cpu;
  694. }
  695. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  696. /* symlink affected CPUs */
  697. for_each_cpu_mask(j, policy->cpus) {
  698. if (j == cpu)
  699. continue;
  700. if (!cpu_online(j))
  701. continue;
  702. dprintk("CPU %u already managed, adding link\n", j);
  703. cpufreq_cpu_get(cpu);
  704. cpu_sys_dev = get_cpu_sysdev(j);
  705. ret = sysfs_create_link(&cpu_sys_dev->kobj, &policy->kobj,
  706. "cpufreq");
  707. if (ret) {
  708. unlock_policy_rwsem_write(cpu);
  709. goto err_out_unregister;
  710. }
  711. }
  712. policy->governor = NULL; /* to assure that the starting sequence is
  713. * run in cpufreq_set_policy */
  714. unlock_policy_rwsem_write(cpu);
  715. /* set default policy */
  716. ret = cpufreq_set_policy(&new_policy);
  717. if (ret) {
  718. dprintk("setting policy failed\n");
  719. goto err_out_unregister;
  720. }
  721. module_put(cpufreq_driver->owner);
  722. dprintk("initialization complete\n");
  723. cpufreq_debug_enable_ratelimit();
  724. return 0;
  725. err_out_unregister:
  726. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  727. for_each_cpu_mask(j, policy->cpus)
  728. cpufreq_cpu_data[j] = NULL;
  729. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  730. kobject_unregister(&policy->kobj);
  731. wait_for_completion(&policy->kobj_unregister);
  732. err_out_driver_exit:
  733. if (cpufreq_driver->exit)
  734. cpufreq_driver->exit(policy);
  735. err_out:
  736. kfree(policy);
  737. nomem_out:
  738. module_put(cpufreq_driver->owner);
  739. module_out:
  740. cpufreq_debug_enable_ratelimit();
  741. return ret;
  742. }
  743. /**
  744. * __cpufreq_remove_dev - remove a CPU device
  745. *
  746. * Removes the cpufreq interface for a CPU device.
  747. * Caller should already have policy_rwsem in write mode for this CPU.
  748. * This routine frees the rwsem before returning.
  749. */
  750. static int __cpufreq_remove_dev (struct sys_device * sys_dev)
  751. {
  752. unsigned int cpu = sys_dev->id;
  753. unsigned long flags;
  754. struct cpufreq_policy *data;
  755. #ifdef CONFIG_SMP
  756. struct sys_device *cpu_sys_dev;
  757. unsigned int j;
  758. #endif
  759. cpufreq_debug_disable_ratelimit();
  760. dprintk("unregistering CPU %u\n", cpu);
  761. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  762. data = cpufreq_cpu_data[cpu];
  763. if (!data) {
  764. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  765. cpufreq_debug_enable_ratelimit();
  766. unlock_policy_rwsem_write(cpu);
  767. return -EINVAL;
  768. }
  769. cpufreq_cpu_data[cpu] = NULL;
  770. #ifdef CONFIG_SMP
  771. /* if this isn't the CPU which is the parent of the kobj, we
  772. * only need to unlink, put and exit
  773. */
  774. if (unlikely(cpu != data->cpu)) {
  775. dprintk("removing link\n");
  776. cpu_clear(cpu, data->cpus);
  777. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  778. sysfs_remove_link(&sys_dev->kobj, "cpufreq");
  779. cpufreq_cpu_put(data);
  780. cpufreq_debug_enable_ratelimit();
  781. unlock_policy_rwsem_write(cpu);
  782. return 0;
  783. }
  784. #endif
  785. if (!kobject_get(&data->kobj)) {
  786. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  787. cpufreq_debug_enable_ratelimit();
  788. unlock_policy_rwsem_write(cpu);
  789. return -EFAULT;
  790. }
  791. #ifdef CONFIG_SMP
  792. /* if we have other CPUs still registered, we need to unlink them,
  793. * or else wait_for_completion below will lock up. Clean the
  794. * cpufreq_cpu_data[] while holding the lock, and remove the sysfs
  795. * links afterwards.
  796. */
  797. if (unlikely(cpus_weight(data->cpus) > 1)) {
  798. for_each_cpu_mask(j, data->cpus) {
  799. if (j == cpu)
  800. continue;
  801. cpufreq_cpu_data[j] = NULL;
  802. }
  803. }
  804. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  805. if (unlikely(cpus_weight(data->cpus) > 1)) {
  806. for_each_cpu_mask(j, data->cpus) {
  807. if (j == cpu)
  808. continue;
  809. dprintk("removing link for cpu %u\n", j);
  810. cpu_sys_dev = get_cpu_sysdev(j);
  811. sysfs_remove_link(&cpu_sys_dev->kobj, "cpufreq");
  812. cpufreq_cpu_put(data);
  813. }
  814. }
  815. #else
  816. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  817. #endif
  818. if (cpufreq_driver->target)
  819. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  820. unlock_policy_rwsem_write(cpu);
  821. kobject_unregister(&data->kobj);
  822. kobject_put(&data->kobj);
  823. /* we need to make sure that the underlying kobj is actually
  824. * not referenced anymore by anybody before we proceed with
  825. * unloading.
  826. */
  827. dprintk("waiting for dropping of refcount\n");
  828. wait_for_completion(&data->kobj_unregister);
  829. dprintk("wait complete\n");
  830. if (cpufreq_driver->exit)
  831. cpufreq_driver->exit(data);
  832. kfree(data);
  833. cpufreq_debug_enable_ratelimit();
  834. return 0;
  835. }
  836. static int cpufreq_remove_dev (struct sys_device * sys_dev)
  837. {
  838. unsigned int cpu = sys_dev->id;
  839. int retval;
  840. if (unlikely(lock_policy_rwsem_write(cpu)))
  841. BUG();
  842. retval = __cpufreq_remove_dev(sys_dev);
  843. return retval;
  844. }
  845. static void handle_update(struct work_struct *work)
  846. {
  847. struct cpufreq_policy *policy =
  848. container_of(work, struct cpufreq_policy, update);
  849. unsigned int cpu = policy->cpu;
  850. dprintk("handle_update for cpu %u called\n", cpu);
  851. cpufreq_update_policy(cpu);
  852. }
  853. /**
  854. * cpufreq_out_of_sync - If actual and saved CPU frequency differs, we're in deep trouble.
  855. * @cpu: cpu number
  856. * @old_freq: CPU frequency the kernel thinks the CPU runs at
  857. * @new_freq: CPU frequency the CPU actually runs at
  858. *
  859. * We adjust to current frequency first, and need to clean up later. So either call
  860. * to cpufreq_update_policy() or schedule handle_update()).
  861. */
  862. static void cpufreq_out_of_sync(unsigned int cpu, unsigned int old_freq,
  863. unsigned int new_freq)
  864. {
  865. struct cpufreq_freqs freqs;
  866. dprintk("Warning: CPU frequency out of sync: cpufreq and timing "
  867. "core thinks of %u, is %u kHz.\n", old_freq, new_freq);
  868. freqs.cpu = cpu;
  869. freqs.old = old_freq;
  870. freqs.new = new_freq;
  871. cpufreq_notify_transition(&freqs, CPUFREQ_PRECHANGE);
  872. cpufreq_notify_transition(&freqs, CPUFREQ_POSTCHANGE);
  873. }
  874. /**
  875. * cpufreq_quick_get - get the CPU frequency (in kHz) from policy->cur
  876. * @cpu: CPU number
  877. *
  878. * This is the last known freq, without actually getting it from the driver.
  879. * Return value will be same as what is shown in scaling_cur_freq in sysfs.
  880. */
  881. unsigned int cpufreq_quick_get(unsigned int cpu)
  882. {
  883. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  884. unsigned int ret_freq = 0;
  885. if (policy) {
  886. if (unlikely(lock_policy_rwsem_read(cpu)))
  887. return ret_freq;
  888. ret_freq = policy->cur;
  889. unlock_policy_rwsem_read(cpu);
  890. cpufreq_cpu_put(policy);
  891. }
  892. return (ret_freq);
  893. }
  894. EXPORT_SYMBOL(cpufreq_quick_get);
  895. static unsigned int __cpufreq_get(unsigned int cpu)
  896. {
  897. struct cpufreq_policy *policy = cpufreq_cpu_data[cpu];
  898. unsigned int ret_freq = 0;
  899. if (!cpufreq_driver->get)
  900. return (ret_freq);
  901. ret_freq = cpufreq_driver->get(cpu);
  902. if (ret_freq && policy->cur &&
  903. !(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  904. /* verify no discrepancy between actual and
  905. saved value exists */
  906. if (unlikely(ret_freq != policy->cur)) {
  907. cpufreq_out_of_sync(cpu, policy->cur, ret_freq);
  908. schedule_work(&policy->update);
  909. }
  910. }
  911. return (ret_freq);
  912. }
  913. /**
  914. * cpufreq_get - get the current CPU frequency (in kHz)
  915. * @cpu: CPU number
  916. *
  917. * Get the CPU current (static) CPU frequency
  918. */
  919. unsigned int cpufreq_get(unsigned int cpu)
  920. {
  921. unsigned int ret_freq = 0;
  922. struct cpufreq_policy *policy = cpufreq_cpu_get(cpu);
  923. if (!policy)
  924. goto out;
  925. if (unlikely(lock_policy_rwsem_read(cpu)))
  926. goto out_policy;
  927. ret_freq = __cpufreq_get(cpu);
  928. unlock_policy_rwsem_read(cpu);
  929. out_policy:
  930. cpufreq_cpu_put(policy);
  931. out:
  932. return (ret_freq);
  933. }
  934. EXPORT_SYMBOL(cpufreq_get);
  935. /**
  936. * cpufreq_suspend - let the low level driver prepare for suspend
  937. */
  938. static int cpufreq_suspend(struct sys_device * sysdev, pm_message_t pmsg)
  939. {
  940. int cpu = sysdev->id;
  941. int ret = 0;
  942. unsigned int cur_freq = 0;
  943. struct cpufreq_policy *cpu_policy;
  944. dprintk("suspending cpu %u\n", cpu);
  945. if (!cpu_online(cpu))
  946. return 0;
  947. /* we may be lax here as interrupts are off. Nonetheless
  948. * we need to grab the correct cpu policy, as to check
  949. * whether we really run on this CPU.
  950. */
  951. cpu_policy = cpufreq_cpu_get(cpu);
  952. if (!cpu_policy)
  953. return -EINVAL;
  954. /* only handle each CPU group once */
  955. if (unlikely(cpu_policy->cpu != cpu)) {
  956. cpufreq_cpu_put(cpu_policy);
  957. return 0;
  958. }
  959. if (cpufreq_driver->suspend) {
  960. ret = cpufreq_driver->suspend(cpu_policy, pmsg);
  961. if (ret) {
  962. printk(KERN_ERR "cpufreq: suspend failed in ->suspend "
  963. "step on CPU %u\n", cpu_policy->cpu);
  964. cpufreq_cpu_put(cpu_policy);
  965. return ret;
  966. }
  967. }
  968. if (cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)
  969. goto out;
  970. if (cpufreq_driver->get)
  971. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  972. if (!cur_freq || !cpu_policy->cur) {
  973. printk(KERN_ERR "cpufreq: suspend failed to assert current "
  974. "frequency is what timing core thinks it is.\n");
  975. goto out;
  976. }
  977. if (unlikely(cur_freq != cpu_policy->cur)) {
  978. struct cpufreq_freqs freqs;
  979. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  980. dprintk("Warning: CPU frequency is %u, "
  981. "cpufreq assumed %u kHz.\n",
  982. cur_freq, cpu_policy->cur);
  983. freqs.cpu = cpu;
  984. freqs.old = cpu_policy->cur;
  985. freqs.new = cur_freq;
  986. srcu_notifier_call_chain(&cpufreq_transition_notifier_list,
  987. CPUFREQ_SUSPENDCHANGE, &freqs);
  988. adjust_jiffies(CPUFREQ_SUSPENDCHANGE, &freqs);
  989. cpu_policy->cur = cur_freq;
  990. }
  991. out:
  992. cpufreq_cpu_put(cpu_policy);
  993. return 0;
  994. }
  995. /**
  996. * cpufreq_resume - restore proper CPU frequency handling after resume
  997. *
  998. * 1.) resume CPUfreq hardware support (cpufreq_driver->resume())
  999. * 2.) if ->target and !CPUFREQ_CONST_LOOPS: verify we're in sync
  1000. * 3.) schedule call cpufreq_update_policy() ASAP as interrupts are
  1001. * restored.
  1002. */
  1003. static int cpufreq_resume(struct sys_device * sysdev)
  1004. {
  1005. int cpu = sysdev->id;
  1006. int ret = 0;
  1007. struct cpufreq_policy *cpu_policy;
  1008. dprintk("resuming cpu %u\n", cpu);
  1009. if (!cpu_online(cpu))
  1010. return 0;
  1011. /* we may be lax here as interrupts are off. Nonetheless
  1012. * we need to grab the correct cpu policy, as to check
  1013. * whether we really run on this CPU.
  1014. */
  1015. cpu_policy = cpufreq_cpu_get(cpu);
  1016. if (!cpu_policy)
  1017. return -EINVAL;
  1018. /* only handle each CPU group once */
  1019. if (unlikely(cpu_policy->cpu != cpu)) {
  1020. cpufreq_cpu_put(cpu_policy);
  1021. return 0;
  1022. }
  1023. if (cpufreq_driver->resume) {
  1024. ret = cpufreq_driver->resume(cpu_policy);
  1025. if (ret) {
  1026. printk(KERN_ERR "cpufreq: resume failed in ->resume "
  1027. "step on CPU %u\n", cpu_policy->cpu);
  1028. cpufreq_cpu_put(cpu_policy);
  1029. return ret;
  1030. }
  1031. }
  1032. if (!(cpufreq_driver->flags & CPUFREQ_CONST_LOOPS)) {
  1033. unsigned int cur_freq = 0;
  1034. if (cpufreq_driver->get)
  1035. cur_freq = cpufreq_driver->get(cpu_policy->cpu);
  1036. if (!cur_freq || !cpu_policy->cur) {
  1037. printk(KERN_ERR "cpufreq: resume failed to assert "
  1038. "current frequency is what timing core "
  1039. "thinks it is.\n");
  1040. goto out;
  1041. }
  1042. if (unlikely(cur_freq != cpu_policy->cur)) {
  1043. struct cpufreq_freqs freqs;
  1044. if (!(cpufreq_driver->flags & CPUFREQ_PM_NO_WARN))
  1045. dprintk("Warning: CPU frequency"
  1046. "is %u, cpufreq assumed %u kHz.\n",
  1047. cur_freq, cpu_policy->cur);
  1048. freqs.cpu = cpu;
  1049. freqs.old = cpu_policy->cur;
  1050. freqs.new = cur_freq;
  1051. srcu_notifier_call_chain(
  1052. &cpufreq_transition_notifier_list,
  1053. CPUFREQ_RESUMECHANGE, &freqs);
  1054. adjust_jiffies(CPUFREQ_RESUMECHANGE, &freqs);
  1055. cpu_policy->cur = cur_freq;
  1056. }
  1057. }
  1058. out:
  1059. schedule_work(&cpu_policy->update);
  1060. cpufreq_cpu_put(cpu_policy);
  1061. return ret;
  1062. }
  1063. static struct sysdev_driver cpufreq_sysdev_driver = {
  1064. .add = cpufreq_add_dev,
  1065. .remove = cpufreq_remove_dev,
  1066. .suspend = cpufreq_suspend,
  1067. .resume = cpufreq_resume,
  1068. };
  1069. /*********************************************************************
  1070. * NOTIFIER LISTS INTERFACE *
  1071. *********************************************************************/
  1072. /**
  1073. * cpufreq_register_notifier - register a driver with cpufreq
  1074. * @nb: notifier function to register
  1075. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1076. *
  1077. * Add a driver to one of two lists: either a list of drivers that
  1078. * are notified about clock rate changes (once before and once after
  1079. * the transition), or a list of drivers that are notified about
  1080. * changes in cpufreq policy.
  1081. *
  1082. * This function may sleep, and has the same return conditions as
  1083. * blocking_notifier_chain_register.
  1084. */
  1085. int cpufreq_register_notifier(struct notifier_block *nb, unsigned int list)
  1086. {
  1087. int ret;
  1088. switch (list) {
  1089. case CPUFREQ_TRANSITION_NOTIFIER:
  1090. ret = srcu_notifier_chain_register(
  1091. &cpufreq_transition_notifier_list, nb);
  1092. break;
  1093. case CPUFREQ_POLICY_NOTIFIER:
  1094. ret = blocking_notifier_chain_register(
  1095. &cpufreq_policy_notifier_list, nb);
  1096. break;
  1097. default:
  1098. ret = -EINVAL;
  1099. }
  1100. return ret;
  1101. }
  1102. EXPORT_SYMBOL(cpufreq_register_notifier);
  1103. /**
  1104. * cpufreq_unregister_notifier - unregister a driver with cpufreq
  1105. * @nb: notifier block to be unregistered
  1106. * @list: CPUFREQ_TRANSITION_NOTIFIER or CPUFREQ_POLICY_NOTIFIER
  1107. *
  1108. * Remove a driver from the CPU frequency notifier list.
  1109. *
  1110. * This function may sleep, and has the same return conditions as
  1111. * blocking_notifier_chain_unregister.
  1112. */
  1113. int cpufreq_unregister_notifier(struct notifier_block *nb, unsigned int list)
  1114. {
  1115. int ret;
  1116. switch (list) {
  1117. case CPUFREQ_TRANSITION_NOTIFIER:
  1118. ret = srcu_notifier_chain_unregister(
  1119. &cpufreq_transition_notifier_list, nb);
  1120. break;
  1121. case CPUFREQ_POLICY_NOTIFIER:
  1122. ret = blocking_notifier_chain_unregister(
  1123. &cpufreq_policy_notifier_list, nb);
  1124. break;
  1125. default:
  1126. ret = -EINVAL;
  1127. }
  1128. return ret;
  1129. }
  1130. EXPORT_SYMBOL(cpufreq_unregister_notifier);
  1131. /*********************************************************************
  1132. * GOVERNORS *
  1133. *********************************************************************/
  1134. int __cpufreq_driver_target(struct cpufreq_policy *policy,
  1135. unsigned int target_freq,
  1136. unsigned int relation)
  1137. {
  1138. int retval = -EINVAL;
  1139. dprintk("target for CPU %u: %u kHz, relation %u\n", policy->cpu,
  1140. target_freq, relation);
  1141. if (cpu_online(policy->cpu) && cpufreq_driver->target)
  1142. retval = cpufreq_driver->target(policy, target_freq, relation);
  1143. return retval;
  1144. }
  1145. EXPORT_SYMBOL_GPL(__cpufreq_driver_target);
  1146. int cpufreq_driver_target(struct cpufreq_policy *policy,
  1147. unsigned int target_freq,
  1148. unsigned int relation)
  1149. {
  1150. int ret;
  1151. policy = cpufreq_cpu_get(policy->cpu);
  1152. if (!policy)
  1153. return -EINVAL;
  1154. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1155. return -EINVAL;
  1156. ret = __cpufreq_driver_target(policy, target_freq, relation);
  1157. unlock_policy_rwsem_write(policy->cpu);
  1158. cpufreq_cpu_put(policy);
  1159. return ret;
  1160. }
  1161. EXPORT_SYMBOL_GPL(cpufreq_driver_target);
  1162. int __cpufreq_driver_getavg(struct cpufreq_policy *policy)
  1163. {
  1164. int ret = 0;
  1165. policy = cpufreq_cpu_get(policy->cpu);
  1166. if (!policy)
  1167. return -EINVAL;
  1168. if (cpu_online(policy->cpu) && cpufreq_driver->getavg)
  1169. ret = cpufreq_driver->getavg(policy->cpu);
  1170. cpufreq_cpu_put(policy);
  1171. return ret;
  1172. }
  1173. EXPORT_SYMBOL_GPL(__cpufreq_driver_getavg);
  1174. /*
  1175. * when "event" is CPUFREQ_GOV_LIMITS
  1176. */
  1177. static int __cpufreq_governor(struct cpufreq_policy *policy,
  1178. unsigned int event)
  1179. {
  1180. int ret;
  1181. if (!try_module_get(policy->governor->owner))
  1182. return -EINVAL;
  1183. dprintk("__cpufreq_governor for CPU %u, event %u\n",
  1184. policy->cpu, event);
  1185. ret = policy->governor->governor(policy, event);
  1186. /* we keep one module reference alive for
  1187. each CPU governed by this CPU */
  1188. if ((event != CPUFREQ_GOV_START) || ret)
  1189. module_put(policy->governor->owner);
  1190. if ((event == CPUFREQ_GOV_STOP) && !ret)
  1191. module_put(policy->governor->owner);
  1192. return ret;
  1193. }
  1194. int cpufreq_register_governor(struct cpufreq_governor *governor)
  1195. {
  1196. int err;
  1197. if (!governor)
  1198. return -EINVAL;
  1199. mutex_lock(&cpufreq_governor_mutex);
  1200. err = -EBUSY;
  1201. if (__find_governor(governor->name) == NULL) {
  1202. err = 0;
  1203. list_add(&governor->governor_list, &cpufreq_governor_list);
  1204. }
  1205. mutex_unlock(&cpufreq_governor_mutex);
  1206. return err;
  1207. }
  1208. EXPORT_SYMBOL_GPL(cpufreq_register_governor);
  1209. void cpufreq_unregister_governor(struct cpufreq_governor *governor)
  1210. {
  1211. if (!governor)
  1212. return;
  1213. mutex_lock(&cpufreq_governor_mutex);
  1214. list_del(&governor->governor_list);
  1215. mutex_unlock(&cpufreq_governor_mutex);
  1216. return;
  1217. }
  1218. EXPORT_SYMBOL_GPL(cpufreq_unregister_governor);
  1219. /*********************************************************************
  1220. * POLICY INTERFACE *
  1221. *********************************************************************/
  1222. /**
  1223. * cpufreq_get_policy - get the current cpufreq_policy
  1224. * @policy: struct cpufreq_policy into which the current cpufreq_policy is written
  1225. *
  1226. * Reads the current cpufreq policy.
  1227. */
  1228. int cpufreq_get_policy(struct cpufreq_policy *policy, unsigned int cpu)
  1229. {
  1230. struct cpufreq_policy *cpu_policy;
  1231. if (!policy)
  1232. return -EINVAL;
  1233. cpu_policy = cpufreq_cpu_get(cpu);
  1234. if (!cpu_policy)
  1235. return -EINVAL;
  1236. memcpy(policy, cpu_policy, sizeof(struct cpufreq_policy));
  1237. cpufreq_cpu_put(cpu_policy);
  1238. return 0;
  1239. }
  1240. EXPORT_SYMBOL(cpufreq_get_policy);
  1241. /*
  1242. * data : current policy.
  1243. * policy : policy to be set.
  1244. */
  1245. static int __cpufreq_set_policy(struct cpufreq_policy *data,
  1246. struct cpufreq_policy *policy)
  1247. {
  1248. int ret = 0;
  1249. cpufreq_debug_disable_ratelimit();
  1250. dprintk("setting new policy for CPU %u: %u - %u kHz\n", policy->cpu,
  1251. policy->min, policy->max);
  1252. memcpy(&policy->cpuinfo, &data->cpuinfo,
  1253. sizeof(struct cpufreq_cpuinfo));
  1254. if (policy->min > data->min && policy->min > policy->max) {
  1255. ret = -EINVAL;
  1256. goto error_out;
  1257. }
  1258. /* verify the cpu speed can be set within this limit */
  1259. ret = cpufreq_driver->verify(policy);
  1260. if (ret)
  1261. goto error_out;
  1262. /* adjust if necessary - all reasons */
  1263. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1264. CPUFREQ_ADJUST, policy);
  1265. /* adjust if necessary - hardware incompatibility*/
  1266. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1267. CPUFREQ_INCOMPATIBLE, policy);
  1268. /* verify the cpu speed can be set within this limit,
  1269. which might be different to the first one */
  1270. ret = cpufreq_driver->verify(policy);
  1271. if (ret)
  1272. goto error_out;
  1273. /* notification of the new policy */
  1274. blocking_notifier_call_chain(&cpufreq_policy_notifier_list,
  1275. CPUFREQ_NOTIFY, policy);
  1276. data->min = policy->min;
  1277. data->max = policy->max;
  1278. dprintk("new min and max freqs are %u - %u kHz\n",
  1279. data->min, data->max);
  1280. if (cpufreq_driver->setpolicy) {
  1281. data->policy = policy->policy;
  1282. dprintk("setting range\n");
  1283. ret = cpufreq_driver->setpolicy(policy);
  1284. } else {
  1285. if (policy->governor != data->governor) {
  1286. /* save old, working values */
  1287. struct cpufreq_governor *old_gov = data->governor;
  1288. dprintk("governor switch\n");
  1289. /* end old governor */
  1290. if (data->governor)
  1291. __cpufreq_governor(data, CPUFREQ_GOV_STOP);
  1292. /* start new governor */
  1293. data->governor = policy->governor;
  1294. if (__cpufreq_governor(data, CPUFREQ_GOV_START)) {
  1295. /* new governor failed, so re-start old one */
  1296. dprintk("starting governor %s failed\n",
  1297. data->governor->name);
  1298. if (old_gov) {
  1299. data->governor = old_gov;
  1300. __cpufreq_governor(data,
  1301. CPUFREQ_GOV_START);
  1302. }
  1303. ret = -EINVAL;
  1304. goto error_out;
  1305. }
  1306. /* might be a policy change, too, so fall through */
  1307. }
  1308. dprintk("governor: change or update limits\n");
  1309. __cpufreq_governor(data, CPUFREQ_GOV_LIMITS);
  1310. }
  1311. error_out:
  1312. cpufreq_debug_enable_ratelimit();
  1313. return ret;
  1314. }
  1315. /**
  1316. * cpufreq_set_policy - set a new CPUFreq policy
  1317. * @policy: policy to be set.
  1318. *
  1319. * Sets a new CPU frequency and voltage scaling policy.
  1320. */
  1321. int cpufreq_set_policy(struct cpufreq_policy *policy)
  1322. {
  1323. int ret = 0;
  1324. struct cpufreq_policy *data;
  1325. if (!policy)
  1326. return -EINVAL;
  1327. data = cpufreq_cpu_get(policy->cpu);
  1328. if (!data)
  1329. return -EINVAL;
  1330. if (unlikely(lock_policy_rwsem_write(policy->cpu)))
  1331. return -EINVAL;
  1332. ret = __cpufreq_set_policy(data, policy);
  1333. data->user_policy.min = data->min;
  1334. data->user_policy.max = data->max;
  1335. data->user_policy.policy = data->policy;
  1336. data->user_policy.governor = data->governor;
  1337. unlock_policy_rwsem_write(policy->cpu);
  1338. cpufreq_cpu_put(data);
  1339. return ret;
  1340. }
  1341. EXPORT_SYMBOL(cpufreq_set_policy);
  1342. /**
  1343. * cpufreq_update_policy - re-evaluate an existing cpufreq policy
  1344. * @cpu: CPU which shall be re-evaluated
  1345. *
  1346. * Usefull for policy notifiers which have different necessities
  1347. * at different times.
  1348. */
  1349. int cpufreq_update_policy(unsigned int cpu)
  1350. {
  1351. struct cpufreq_policy *data = cpufreq_cpu_get(cpu);
  1352. struct cpufreq_policy policy;
  1353. int ret = 0;
  1354. if (!data)
  1355. return -ENODEV;
  1356. if (unlikely(lock_policy_rwsem_write(cpu)))
  1357. return -EINVAL;
  1358. dprintk("updating policy for CPU %u\n", cpu);
  1359. memcpy(&policy, data, sizeof(struct cpufreq_policy));
  1360. policy.min = data->user_policy.min;
  1361. policy.max = data->user_policy.max;
  1362. policy.policy = data->user_policy.policy;
  1363. policy.governor = data->user_policy.governor;
  1364. /* BIOS might change freq behind our back
  1365. -> ask driver for current freq and notify governors about a change */
  1366. if (cpufreq_driver->get) {
  1367. policy.cur = cpufreq_driver->get(cpu);
  1368. if (!data->cur) {
  1369. dprintk("Driver did not initialize current freq");
  1370. data->cur = policy.cur;
  1371. } else {
  1372. if (data->cur != policy.cur)
  1373. cpufreq_out_of_sync(cpu, data->cur,
  1374. policy.cur);
  1375. }
  1376. }
  1377. ret = __cpufreq_set_policy(data, &policy);
  1378. unlock_policy_rwsem_write(cpu);
  1379. cpufreq_cpu_put(data);
  1380. return ret;
  1381. }
  1382. EXPORT_SYMBOL(cpufreq_update_policy);
  1383. static int cpufreq_cpu_callback(struct notifier_block *nfb,
  1384. unsigned long action, void *hcpu)
  1385. {
  1386. unsigned int cpu = (unsigned long)hcpu;
  1387. struct sys_device *sys_dev;
  1388. struct cpufreq_policy *policy;
  1389. sys_dev = get_cpu_sysdev(cpu);
  1390. if (sys_dev) {
  1391. switch (action) {
  1392. case CPU_ONLINE:
  1393. cpufreq_add_dev(sys_dev);
  1394. break;
  1395. case CPU_DOWN_PREPARE:
  1396. if (unlikely(lock_policy_rwsem_write(cpu)))
  1397. BUG();
  1398. policy = cpufreq_cpu_data[cpu];
  1399. if (policy) {
  1400. __cpufreq_driver_target(policy, policy->min,
  1401. CPUFREQ_RELATION_H);
  1402. }
  1403. __cpufreq_remove_dev(sys_dev);
  1404. break;
  1405. case CPU_DOWN_FAILED:
  1406. cpufreq_add_dev(sys_dev);
  1407. break;
  1408. }
  1409. }
  1410. return NOTIFY_OK;
  1411. }
  1412. static struct notifier_block __cpuinitdata cpufreq_cpu_notifier =
  1413. {
  1414. .notifier_call = cpufreq_cpu_callback,
  1415. };
  1416. /*********************************************************************
  1417. * REGISTER / UNREGISTER CPUFREQ DRIVER *
  1418. *********************************************************************/
  1419. /**
  1420. * cpufreq_register_driver - register a CPU Frequency driver
  1421. * @driver_data: A struct cpufreq_driver containing the values#
  1422. * submitted by the CPU Frequency driver.
  1423. *
  1424. * Registers a CPU Frequency driver to this core code. This code
  1425. * returns zero on success, -EBUSY when another driver got here first
  1426. * (and isn't unregistered in the meantime).
  1427. *
  1428. */
  1429. int cpufreq_register_driver(const struct cpufreq_driver *driver_data)
  1430. {
  1431. unsigned long flags;
  1432. int ret;
  1433. if (!driver_data || !driver_data->verify || !driver_data->init ||
  1434. ((!driver_data->setpolicy) && (!driver_data->target)))
  1435. return -EINVAL;
  1436. dprintk("trying to register driver %s\n", driver_data->name);
  1437. if (driver_data->setpolicy)
  1438. driver_data->flags |= CPUFREQ_CONST_LOOPS;
  1439. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1440. if (cpufreq_driver) {
  1441. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1442. return -EBUSY;
  1443. }
  1444. cpufreq_driver = driver_data;
  1445. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1446. ret = sysdev_driver_register(&cpu_sysdev_class,&cpufreq_sysdev_driver);
  1447. if ((!ret) && !(cpufreq_driver->flags & CPUFREQ_STICKY)) {
  1448. int i;
  1449. ret = -ENODEV;
  1450. /* check for at least one working CPU */
  1451. for (i=0; i<NR_CPUS; i++)
  1452. if (cpufreq_cpu_data[i])
  1453. ret = 0;
  1454. /* if all ->init() calls failed, unregister */
  1455. if (ret) {
  1456. dprintk("no CPU initialized for driver %s\n",
  1457. driver_data->name);
  1458. sysdev_driver_unregister(&cpu_sysdev_class,
  1459. &cpufreq_sysdev_driver);
  1460. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1461. cpufreq_driver = NULL;
  1462. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1463. }
  1464. }
  1465. if (!ret) {
  1466. register_hotcpu_notifier(&cpufreq_cpu_notifier);
  1467. dprintk("driver %s up and running\n", driver_data->name);
  1468. cpufreq_debug_enable_ratelimit();
  1469. }
  1470. return (ret);
  1471. }
  1472. EXPORT_SYMBOL_GPL(cpufreq_register_driver);
  1473. /**
  1474. * cpufreq_unregister_driver - unregister the current CPUFreq driver
  1475. *
  1476. * Unregister the current CPUFreq driver. Only call this if you have
  1477. * the right to do so, i.e. if you have succeeded in initialising before!
  1478. * Returns zero if successful, and -EINVAL if the cpufreq_driver is
  1479. * currently not initialised.
  1480. */
  1481. int cpufreq_unregister_driver(const struct cpufreq_driver *driver)
  1482. {
  1483. unsigned long flags;
  1484. cpufreq_debug_disable_ratelimit();
  1485. if (!cpufreq_driver || (driver != cpufreq_driver)) {
  1486. cpufreq_debug_enable_ratelimit();
  1487. return -EINVAL;
  1488. }
  1489. dprintk("unregistering driver %s\n", driver->name);
  1490. sysdev_driver_unregister(&cpu_sysdev_class, &cpufreq_sysdev_driver);
  1491. unregister_hotcpu_notifier(&cpufreq_cpu_notifier);
  1492. spin_lock_irqsave(&cpufreq_driver_lock, flags);
  1493. cpufreq_driver = NULL;
  1494. spin_unlock_irqrestore(&cpufreq_driver_lock, flags);
  1495. return 0;
  1496. }
  1497. EXPORT_SYMBOL_GPL(cpufreq_unregister_driver);
  1498. static int __init cpufreq_core_init(void)
  1499. {
  1500. int cpu;
  1501. for_each_possible_cpu(cpu) {
  1502. per_cpu(policy_cpu, cpu) = -1;
  1503. init_rwsem(&per_cpu(cpu_policy_rwsem, cpu));
  1504. }
  1505. return 0;
  1506. }
  1507. core_initcall(cpufreq_core_init);